Skip to content

Commit 54a0f94

Browse files
Zubii12ujas-m-simformsolutions
authored andcommitted
Implement audio speed
1 parent 142855e commit 54a0f94

File tree

9 files changed

+62
-0
lines changed

9 files changed

+62
-0
lines changed

android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ class AudioPlayer(
169169
}
170170
}
171171

172+
fun setRate(rate: Float?, result: MethodChannel.Result) {
173+
try {
174+
if (rate != null) {
175+
player?.setPlaybackSpeed(rate)
176+
result.success(true)
177+
} else {
178+
result.success(false)
179+
}
180+
} catch (e: Exception) {
181+
result.success(false)
182+
}
183+
}
184+
172185
private fun startListening(result: MethodChannel.Result) {
173186
runnable = object : Runnable {
174187
override fun run() {

android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
144144
result.error(Constants.LOG_TAG, "Player key can't be null", "")
145145
}
146146
}
147+
Constants.setRate -> {
148+
val rate = call.argument(Constants.rate) as Double?
149+
val key = call.argument(Constants.playerKey) as String?
150+
if (key != null) {
151+
audioPlayers[key]?.setRate(rate?.toFloat(), result)
152+
} else {
153+
result.error(Constants.LOG_TAG, "Player key can't be null", "")
154+
}
155+
}
147156
Constants.getDuration -> {
148157
val type =
149158
if ((call.argument(Constants.durationType) as Int?) == 0) DurationType.Current else DurationType.Max

android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ object Constants {
4949
const val progress = "progress"
5050
const val setVolume = "setVolume"
5151
const val volume = "volume"
52+
const val setRate = "setRate"
53+
const val rate = "rate"
5254
const val getDuration = "getDuration"
5355
const val durationType = "durationType"
5456
const val playerKey = "playerKey"

ios/Classes/AudioPlayer.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
3333
} catch {
3434
result(FlutterError(code: Constants.audioWaveforms, message: "Failed to prepare player", details: error.localizedDescription))
3535
}
36+
player?.enableRate = true
37+
player?.rate = 1.0
3638
player?.prepareToPlay()
3739
player?.volume = Float(volume ?? 1.0)
3840
result(true)
@@ -110,6 +112,11 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
110112
result(true)
111113
}
112114

115+
func setRate(_ rate: Double?, _ result: @escaping FlutterResult) {
116+
player?.rate = Float(rate ?? 1.0);
117+
result(true)
118+
}
119+
113120
func seekTo(_ time: Int?, _ result: @escaping FlutterResult) {
114121
if(time != nil) {
115122
player?.currentTime = Double(time! / 1000)

ios/Classes/SwiftAudioWaveformsPlugin.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public class SwiftAudioWaveformsPlugin: NSObject, FlutterPlugin {
101101
} else {
102102
result(FlutterError(code: Constants.audioWaveforms, message: "Can not set volume", details: "Player key is null"))
103103
}
104+
case Constants.setRate:
105+
let key = args?[Constants.playerKey] as? String
106+
if(key != nil){
107+
audioPlayers[key!]?.setRate(args?[Constants.rate] as? Double,result)
108+
} else {
109+
result(FlutterError(code: Constants.audioWaveforms, message: "Can not set rate", details: "Player key is null"))
110+
}
104111
case Constants.getDuration:
105112
let type = args?[Constants.durationType] as? Int
106113
let key = args?[Constants.playerKey] as? String

ios/Classes/Utils.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ struct Constants {
4040
static let seekTo = "seekTo"
4141
static let progress = "progress"
4242
static let setVolume = "setVolume"
43+
static let setRate = "setRate"
44+
static let rate = "rate"
4345
static let volume = "volume"
4446
static let getDuration = "getDuration"
4547
static let durationType = "durationType"

lib/src/base/audio_waveforms_interface.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ class AudioWaveformsInterface {
156156
});
157157
return result ?? false;
158158
}
159+
160+
///platform call to set rate
161+
Future<bool> setRate(double rate, String key) async {
162+
var result = await _methodChannel.invokeMethod(Constants.setRate, {
163+
Constants.rate: rate,
164+
Constants.playerKey: key,
165+
});
166+
return result ?? false;
167+
}
159168

160169
///platform call to seek audio at provided position
161170
Future<bool> seekTo(String key, int progress) async {

lib/src/base/constants.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Constants {
2626
static const String progress = "progress";
2727
static const String setVolume = "setVolume";
2828
static const String volume = "volume";
29+
static const String setRate = "setRate";
30+
static const String rate = "rate";
2931
static const String rightVolume = "rightVolume";
3032
static const String getDuration = "getDuration";
3133
static const String durationType = "durationType";

lib/src/controllers/player_controller.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ class PlayerController extends ChangeNotifier {
237237
return result;
238238
}
239239

240+
/// Sets rate for this player. Doesn't throw Exception.
241+
/// Returns false if it couldn't set the rate.
242+
///
243+
/// Default to 1.0
244+
245+
Future<bool> setRate(double rate) async {
246+
final result =
247+
await AudioWaveformsInterface.instance.setRate(rate, playerKey);
248+
return result;
249+
}
250+
240251
/// Returns maximum duration for [DurationType.max] and
241252
/// current duration for [DurationType.current] for playing media.
242253
/// The duration is in milliseconds, if no duration is available

0 commit comments

Comments
 (0)