Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(UNT-T32558): current duration update on start, pause, stop player method #159

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions android/src/main/java/com/audiowaveform/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class AudioPlayer(
if (requestAudioFocus()) {
player.playWhenReady = true
player.play()
emitCurrentDuration()
promise.resolve(true)
startListening(promise)}
else {
Expand All @@ -235,6 +236,7 @@ class AudioPlayer(

fun stop() {
stopListening()
emitCurrentDuration()
if (playerListener != null) {
player.removeListener(playerListener!!)
}
Expand All @@ -248,6 +250,7 @@ class AudioPlayer(
try {
stopListening()
player.pause()
emitCurrentDuration()
abandonAudioFocus()
promise?.resolve(true)
} catch (e: Exception) {
Expand All @@ -273,17 +276,22 @@ class AudioPlayer(
return validateAndSetPlaybackSpeed(player, speed)
}


fun emitCurrentDuration() {
val currentPosition = player.currentPosition.toString()
val args: WritableMap = Arguments.createMap()
args.putString(Constants.currentDuration, currentPosition)
args.putString(Constants.playerKey, key)
if (isComponentMounted) {
appContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit("onCurrentDuration", args)
}
}

private fun startListening(promise: Promise) {
try {
audioPlaybackListener = object : CountDownTimer(player.duration, UpdateFrequency.Low.value) {
override fun onTick(millisUntilFinished: Long) {
val currentPosition = player.currentPosition.toString()
val args: WritableMap = Arguments.createMap()
args.putString(Constants.currentDuration, currentPosition)
args.putString(Constants.playerKey, key)
if (isComponentMounted) {
appContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit("onCurrentDuration", args)
}
emitCurrentDuration()
}
override fun onFinish() {}
}.start()
Expand Down
7 changes: 5 additions & 2 deletions ios/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,22 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
player?.play()
player?.delegate = self
player?.rate = Float(speed)
timerUpdate()
startListening()
result(player?.isPlaying)
}

func pausePlayer(result: @escaping RCTPromiseResolveBlock) {
stopListening()
player?.pause()
timerUpdate()
result(true)
}

func stopPlayer() {
stopListening()
player?.stop()
timerUpdate()
player = nil
timer = nil
}
Expand Down Expand Up @@ -139,7 +142,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
}
}

@objc func timerUpdate(_ sender:Timer) {
@objc func timerUpdate() {
let ms = (self.player?.currentTime ?? 0) * 1000
self.sendEvent(withName: Constants.onCurrentDuration, body: [ Constants.currentDuration: Int(ms), Constants.playerKey: self.playerKey] as [String : Any])
}
Expand All @@ -148,7 +151,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
stopListening()
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else {return }
strongSelf.timer = Timer.scheduledTimer(timeInterval: TimeInterval((Float(strongSelf.updateFrequency.rawValue) / 1000)), target: strongSelf, selector: #selector(strongSelf.timerUpdate(_:)), userInfo: nil, repeats: true)
strongSelf.timer = Timer.scheduledTimer(timeInterval: TimeInterval((Float(strongSelf.updateFrequency.rawValue) / 1000)), target: strongSelf, selector: #selector(strongSelf.timerUpdate), userInfo: nil, repeats: true)
}
}

Expand Down