Skip to content

Commit

Permalink
Send current duration when seeked, paused, or stopped (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbajorek authored Oct 14, 2024
1 parent 927df84 commit fb0612c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
22 changes: 12 additions & 10 deletions android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class AudioPlayer(
fun seekToPosition(result: MethodChannel.Result, progress: Long?) {
if (progress != null) {
player?.seekTo(progress)
sendCurrentDuration()
result.success(true)
} else {
result.success(false)
Expand Down Expand Up @@ -187,16 +188,8 @@ class AudioPlayer(
private fun startListening(result: MethodChannel.Result) {
runnable = object : Runnable {
override fun run() {
val currentPosition = player?.currentPosition
if (currentPosition != null) {
val args: MutableMap<String, Any?> = HashMap()
args[Constants.current] = currentPosition
args[Constants.playerKey] = key
methodChannel.invokeMethod(Constants.onCurrentDuration, args)
handler.postDelayed(this, updateFrequency)
} else {
result.error(Constants.LOG_TAG, "Can't get current Position of player", "")
}
sendCurrentDuration()
handler.postDelayed(this, updateFrequency)
}
}
handler.post(runnable!!)
Expand All @@ -205,5 +198,14 @@ class AudioPlayer(

private fun stopListening() {
runnable?.let { handler.removeCallbacks(it) }
sendCurrentDuration()
}

private fun sendCurrentDuration() {
val currentPosition = player?.currentPosition ?: 0
val args: MutableMap<String, Any?> = HashMap()
args[Constants.current] = currentPosition
args[Constants.playerKey] = key
methodChannel.invokeMethod(Constants.onCurrentDuration, args)
}
}
10 changes: 8 additions & 2 deletions ios/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
func seekTo(_ time: Int?, _ result: @escaping FlutterResult) {
if(time != nil) {
player?.currentTime = Double(time! / 1000)
sendCurrentDuration()
result(true)
} else {
result(false)
Expand All @@ -129,8 +130,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
func startListening() {
if #available(iOS 10.0, *) {
timer = Timer.scheduledTimer(withTimeInterval: (Double(updateFrequency) / 1000), repeats: true, block: { _ in
let ms = (self.player?.currentTime ?? 0) * 1000
self.flutterChannel.invokeMethod(Constants.onCurrentDuration, arguments: [Constants.current: Int(ms), Constants.playerKey: self.playerKey])
self.sendCurrentDuration()
})
} else {
// Fallback on earlier versions
Expand All @@ -140,5 +140,11 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
func stopListening() {
timer?.invalidate()
timer = nil
sendCurrentDuration()
}

func sendCurrentDuration() {
let ms = (player?.currentTime ?? 0) * 1000
flutterChannel.invokeMethod(Constants.onCurrentDuration, arguments: [Constants.current: Int(ms), Constants.playerKey: playerKey])
}
}

0 comments on commit fb0612c

Please sign in to comment.