Skip to content

Commit fb0612c

Browse files
authored
Send current duration when seeked, paused, or stopped (#320)
1 parent 927df84 commit fb0612c

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class AudioPlayer(
8989
fun seekToPosition(result: MethodChannel.Result, progress: Long?) {
9090
if (progress != null) {
9191
player?.seekTo(progress)
92+
sendCurrentDuration()
9293
result.success(true)
9394
} else {
9495
result.success(false)
@@ -187,16 +188,8 @@ class AudioPlayer(
187188
private fun startListening(result: MethodChannel.Result) {
188189
runnable = object : Runnable {
189190
override fun run() {
190-
val currentPosition = player?.currentPosition
191-
if (currentPosition != null) {
192-
val args: MutableMap<String, Any?> = HashMap()
193-
args[Constants.current] = currentPosition
194-
args[Constants.playerKey] = key
195-
methodChannel.invokeMethod(Constants.onCurrentDuration, args)
196-
handler.postDelayed(this, updateFrequency)
197-
} else {
198-
result.error(Constants.LOG_TAG, "Can't get current Position of player", "")
199-
}
191+
sendCurrentDuration()
192+
handler.postDelayed(this, updateFrequency)
200193
}
201194
}
202195
handler.post(runnable!!)
@@ -205,5 +198,14 @@ class AudioPlayer(
205198

206199
private fun stopListening() {
207200
runnable?.let { handler.removeCallbacks(it) }
201+
sendCurrentDuration()
202+
}
203+
204+
private fun sendCurrentDuration() {
205+
val currentPosition = player?.currentPosition ?: 0
206+
val args: MutableMap<String, Any?> = HashMap()
207+
args[Constants.current] = currentPosition
208+
args[Constants.playerKey] = key
209+
methodChannel.invokeMethod(Constants.onCurrentDuration, args)
208210
}
209211
}

ios/Classes/AudioPlayer.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
120120
func seekTo(_ time: Int?, _ result: @escaping FlutterResult) {
121121
if(time != nil) {
122122
player?.currentTime = Double(time! / 1000)
123+
sendCurrentDuration()
123124
result(true)
124125
} else {
125126
result(false)
@@ -129,8 +130,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
129130
func startListening() {
130131
if #available(iOS 10.0, *) {
131132
timer = Timer.scheduledTimer(withTimeInterval: (Double(updateFrequency) / 1000), repeats: true, block: { _ in
132-
let ms = (self.player?.currentTime ?? 0) * 1000
133-
self.flutterChannel.invokeMethod(Constants.onCurrentDuration, arguments: [Constants.current: Int(ms), Constants.playerKey: self.playerKey])
133+
self.sendCurrentDuration()
134134
})
135135
} else {
136136
// Fallback on earlier versions
@@ -140,5 +140,11 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
140140
func stopListening() {
141141
timer?.invalidate()
142142
timer = nil
143+
sendCurrentDuration()
144+
}
145+
146+
func sendCurrentDuration() {
147+
let ms = (player?.currentTime ?? 0) * 1000
148+
flutterChannel.invokeMethod(Constants.onCurrentDuration, arguments: [Constants.current: Int(ms), Constants.playerKey: playerKey])
143149
}
144150
}

0 commit comments

Comments
 (0)