Skip to content

Commit 4a93952

Browse files
Merge pull request #159 from SimformSolutionsPvtLtd/feature/UNT-T32558_current_duration_update
feat(UNT-T32558): current duration update on start, pause, stop player method
2 parents 0eb52eb + 4f1629a commit 4a93952

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

android/src/main/java/com/audiowaveform/AudioPlayer.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class AudioPlayer(
223223
if (requestAudioFocus()) {
224224
player.playWhenReady = true
225225
player.play()
226+
emitCurrentDuration()
226227
promise.resolve(true)
227228
startListening(promise)}
228229
else {
@@ -235,6 +236,7 @@ class AudioPlayer(
235236

236237
fun stop() {
237238
stopListening()
239+
emitCurrentDuration()
238240
if (playerListener != null) {
239241
player.removeListener(playerListener!!)
240242
}
@@ -248,6 +250,7 @@ class AudioPlayer(
248250
try {
249251
stopListening()
250252
player.pause()
253+
emitCurrentDuration()
251254
abandonAudioFocus()
252255
promise?.resolve(true)
253256
} catch (e: Exception) {
@@ -273,17 +276,22 @@ class AudioPlayer(
273276
return validateAndSetPlaybackSpeed(player, speed)
274277
}
275278

279+
280+
fun emitCurrentDuration() {
281+
val currentPosition = player.currentPosition.toString()
282+
val args: WritableMap = Arguments.createMap()
283+
args.putString(Constants.currentDuration, currentPosition)
284+
args.putString(Constants.playerKey, key)
285+
if (isComponentMounted) {
286+
appContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit("onCurrentDuration", args)
287+
}
288+
}
289+
276290
private fun startListening(promise: Promise) {
277291
try {
278292
audioPlaybackListener = object : CountDownTimer(player.duration, UpdateFrequency.Low.value) {
279293
override fun onTick(millisUntilFinished: Long) {
280-
val currentPosition = player.currentPosition.toString()
281-
val args: WritableMap = Arguments.createMap()
282-
args.putString(Constants.currentDuration, currentPosition)
283-
args.putString(Constants.playerKey, key)
284-
if (isComponentMounted) {
285-
appContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit("onCurrentDuration", args)
286-
}
294+
emitCurrentDuration()
287295
}
288296
override fun onFinish() {}
289297
}.start()

ios/AudioPlayer.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,22 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
9898
player?.play()
9999
player?.delegate = self
100100
player?.rate = Float(speed)
101+
timerUpdate()
101102
startListening()
102103
result(player?.isPlaying)
103104
}
104105

105106
func pausePlayer(result: @escaping RCTPromiseResolveBlock) {
106107
stopListening()
107108
player?.pause()
109+
timerUpdate()
108110
result(true)
109111
}
110112

111113
func stopPlayer() {
112114
stopListening()
113115
player?.stop()
116+
timerUpdate()
114117
player = nil
115118
timer = nil
116119
}
@@ -139,7 +142,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
139142
}
140143
}
141144

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

0 commit comments

Comments
 (0)