Skip to content

Commit

Permalink
Implement audio speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Zubii12 authored and ujas-m-simformsolutions committed Mar 5, 2024
1 parent 142855e commit 54a0f94
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 0 deletions.
13 changes: 13 additions & 0 deletions android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ class AudioPlayer(
}
}

fun setRate(rate: Float?, result: MethodChannel.Result) {
try {
if (rate != null) {
player?.setPlaybackSpeed(rate)
result.success(true)
} else {
result.success(false)
}
} catch (e: Exception) {
result.success(false)
}
}

private fun startListening(result: MethodChannel.Result) {
runnable = object : Runnable {
override fun run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
result.error(Constants.LOG_TAG, "Player key can't be null", "")
}
}
Constants.setRate -> {
val rate = call.argument(Constants.rate) as Double?
val key = call.argument(Constants.playerKey) as String?
if (key != null) {
audioPlayers[key]?.setRate(rate?.toFloat(), result)
} else {
result.error(Constants.LOG_TAG, "Player key can't be null", "")
}
}
Constants.getDuration -> {
val type =
if ((call.argument(Constants.durationType) as Int?) == 0) DurationType.Current else DurationType.Max
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ object Constants {
const val progress = "progress"
const val setVolume = "setVolume"
const val volume = "volume"
const val setRate = "setRate"
const val rate = "rate"
const val getDuration = "getDuration"
const val durationType = "durationType"
const val playerKey = "playerKey"
Expand Down
7 changes: 7 additions & 0 deletions ios/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
} catch {
result(FlutterError(code: Constants.audioWaveforms, message: "Failed to prepare player", details: error.localizedDescription))
}
player?.enableRate = true
player?.rate = 1.0
player?.prepareToPlay()
player?.volume = Float(volume ?? 1.0)
result(true)
Expand Down Expand Up @@ -110,6 +112,11 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
result(true)
}

func setRate(_ rate: Double?, _ result: @escaping FlutterResult) {
player?.rate = Float(rate ?? 1.0);
result(true)
}

func seekTo(_ time: Int?, _ result: @escaping FlutterResult) {
if(time != nil) {
player?.currentTime = Double(time! / 1000)
Expand Down
7 changes: 7 additions & 0 deletions ios/Classes/SwiftAudioWaveformsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ public class SwiftAudioWaveformsPlugin: NSObject, FlutterPlugin {
} else {
result(FlutterError(code: Constants.audioWaveforms, message: "Can not set volume", details: "Player key is null"))
}
case Constants.setRate:
let key = args?[Constants.playerKey] as? String
if(key != nil){
audioPlayers[key!]?.setRate(args?[Constants.rate] as? Double,result)
} else {
result(FlutterError(code: Constants.audioWaveforms, message: "Can not set rate", details: "Player key is null"))
}
case Constants.getDuration:
let type = args?[Constants.durationType] as? Int
let key = args?[Constants.playerKey] as? String
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct Constants {
static let seekTo = "seekTo"
static let progress = "progress"
static let setVolume = "setVolume"
static let setRate = "setRate"
static let rate = "rate"
static let volume = "volume"
static let getDuration = "getDuration"
static let durationType = "durationType"
Expand Down
9 changes: 9 additions & 0 deletions lib/src/base/audio_waveforms_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ class AudioWaveformsInterface {
});
return result ?? false;
}

///platform call to set rate
Future<bool> setRate(double rate, String key) async {
var result = await _methodChannel.invokeMethod(Constants.setRate, {
Constants.rate: rate,
Constants.playerKey: key,
});
return result ?? false;
}

///platform call to seek audio at provided position
Future<bool> seekTo(String key, int progress) async {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/base/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Constants {
static const String progress = "progress";
static const String setVolume = "setVolume";
static const String volume = "volume";
static const String setRate = "setRate";
static const String rate = "rate";
static const String rightVolume = "rightVolume";
static const String getDuration = "getDuration";
static const String durationType = "durationType";
Expand Down
11 changes: 11 additions & 0 deletions lib/src/controllers/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ class PlayerController extends ChangeNotifier {
return result;
}

/// Sets rate for this player. Doesn't throw Exception.
/// Returns false if it couldn't set the rate.
///
/// Default to 1.0
Future<bool> setRate(double rate) async {
final result =
await AudioWaveformsInterface.instance.setRate(rate, playerKey);
return result;
}

/// Returns maximum duration for [DurationType.max] and
/// current duration for [DurationType.current] for playing media.
/// The duration is in milliseconds, if no duration is available
Expand Down

0 comments on commit 54a0f94

Please sign in to comment.