Skip to content

Commit

Permalink
fix(UNT-T29402): promise leaking and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nilesh-simform committed Oct 8, 2024
1 parent 6464e2d commit 91f9865
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
15 changes: 3 additions & 12 deletions android/src/main/java/com/audiowaveform/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,14 @@ class AudioPlayer(
}
}

fun stop(promise: Promise) {
fun stop() {
stopListening()
if (playerListener != null) {
player.removeListener(playerListener!!)
}
isPlayerPrepared = false
player.stop()
player.release()
promise.resolve(true)
}

fun pause(promise: Promise) {
Expand All @@ -180,16 +179,8 @@ class AudioPlayer(
}
}

fun setPlaybackSpeed(speed: Float?, promise: Promise) {
try {
// Call the custom function to validate and set the playback speed
val success = validateAndSetPlaybackSpeed(player, speed)
promise.resolve(success) // Resolve the promise with success

} catch (e: Exception) {
// Handle any exceptions and reject the promise
promise.reject("setPlaybackSpeed Error", e.toString())
}
fun setPlaybackSpeed(speed: Float?): Boolean {
return validateAndSetPlaybackSpeed(player, speed)
}

private fun startListening(promise: Promise) {
Expand Down
60 changes: 36 additions & 24 deletions android/src/main/java/com/audiowaveform/AudioWaveformModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
fun stopPlayer(obj: ReadableMap, promise: Promise) {
val key = obj.getString(Constants.playerKey)
if (key != null) {
audioPlayers[key]?.stop(promise)
audioPlayers[key]?.stop()
audioPlayers[key] = null // Release the player after stopping it
promise.resolve(true)
} else {
promise.reject("stopPlayer Error", "Player key can't be null")
}
Expand All @@ -191,19 +192,24 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav

@ReactMethod
fun seekToPlayer(obj: ReadableMap, promise: Promise) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val progress = obj.getInt(Constants.progress)
val key = obj.getString(Constants.playerKey)
if (key != null) {
audioPlayers[key]?.seekToPosition(progress.toLong(), promise)
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val progress = obj.getInt(Constants.progress)
val key = obj.getString(Constants.playerKey)
if (key != null) {
audioPlayers[key]?.seekToPosition(progress.toLong(), promise)
} else {
promise.reject("seekTo Error", "Player key can't be null")
}
} else {
promise.reject("seekTo Error", "Player key can't be null")
Log.e(
Constants.LOG_TAG,
"Minimum android O is required for seekTo function to works"
)
promise.resolve(false)
}
} else {
Log.e(
Constants.LOG_TAG,
"Minimum android O is required for seekTo function to works"
)
} catch(e: Exception) {
promise.reject("seekTo Error", e.toString())
}
}

Expand Down Expand Up @@ -245,25 +251,31 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
@ReactMethod
fun stopAllPlayers(promise: Promise) {
for ((key, _) in audioPlayers) {
audioPlayers[key]?.stop(promise)
audioPlayers[key]?.stop()
audioPlayers[key] = null
}
promise.resolve(true)
}

@ReactMethod
fun setPlaybackSpeed(obj: ReadableMap, promise: Promise) {
// If the key doesn't exist or if the value is null or undefined, set default speed to 1.0
val speed = if (!obj.hasKey(Constants.speed) || obj.isNull(Constants.speed)) {
1.0f // Set default speed to 1.0 if null, undefined, or missing
} else {
obj.getDouble(Constants.speed).toFloat()
}
try {
// If the key doesn't exist or if the value is null or undefined, set default speed to 1.0
val speed = if (!obj.hasKey(Constants.speed) || obj.isNull(Constants.speed)) {
1.0f // Set default speed to 1.0 if null, undefined, or missing
} else {
obj.getDouble(Constants.speed).toFloat()
}

val key = obj.getString(Constants.playerKey)
if (key != null) {
audioPlayers[key]?.setPlaybackSpeed(speed, promise)
} else {
promise.reject("setPlaybackSpeed Error", "Player key can't be null")
val key = obj.getString(Constants.playerKey)
if (key != null) {
val status = audioPlayers[key]?.setPlaybackSpeed(speed)
promise.resolve(status ?: false)
} else {
promise.reject("setPlaybackSpeed Error", "Player key can't be null")
}
} catch(e: Exception) {
promise.reject("setPlaybackSpeed Error", e.toString())
}
}

Expand Down
9 changes: 4 additions & 5 deletions ios/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
result(true)
}

func stopPlayer(result: @escaping RCTPromiseResolveBlock) {
func stopPlayer() {
stopListening()
player?.stop()
player = nil
timer = nil
result(true)
}

func getDuration(_ type: DurationType, _ result: @escaping RCTPromiseResolveBlock) {
Expand Down Expand Up @@ -153,13 +152,13 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
}
}

func setPlaybackSpeed(_ speed: Float, _ result: @escaping RCTPromiseResolveBlock) {
func setPlaybackSpeed(_ speed: Float) -> Bool {
if let player = player {
player.enableRate = true
player.rate = Float(speed)
result(true)
return true
} else {
result(false)
return false
}
}

Expand Down
8 changes: 5 additions & 3 deletions ios/AudioWaveform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ class AudioWaveform: RCTEventEmitter {
@objc func stopPlayer(_ args: NSDictionary?, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
let key = args?[Constants.playerKey] as? String
if(key != nil){
audioPlayers[key!]?.stopPlayer(result: resolve)
audioPlayers[key!]?.stopPlayer()
audioPlayers[key!] = nil // Release the player after stopping it
resolve(true)
} else {
reject(Constants.audioWaveforms, "Can not stop player, Player key is null", NSError())
}
Expand Down Expand Up @@ -229,7 +230,7 @@ class AudioWaveform: RCTEventEmitter {

@objc func stopAllPlayers(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
for (playerKey,_) in audioPlayers{
audioPlayers[playerKey]?.stopPlayer(result: resolve)
audioPlayers[playerKey]?.stopPlayer()
audioPlayers[playerKey] = nil
}
resolve(true)
Expand Down Expand Up @@ -259,7 +260,8 @@ class AudioWaveform: RCTEventEmitter {
let speed = (args?[Constants.speed] as? NSNumber)?.floatValue ?? 1.0

if(key != nil){
audioPlayers[key!]?.setPlaybackSpeed(speed, resolve)
let status = audioPlayers[key!]?.setPlaybackSpeed(speed)
resolve(status)
} else {
reject(Constants.audioWaveforms, "Can not pause player, Player key is null", NSError())
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/Waveform/Waveform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
onPlayerStateChange,
onRecorderStateChange,
onPanStateChange = () => {},
onError,
onError = () => {},
onCurrentProgressChange = () => {},
candleHeightScale = 3,
onChangeWaveformLoadState,
Expand Down Expand Up @@ -448,7 +448,6 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
if (data.finishType === FinishMode.stop) {
setPlayerState(PlayerState.stopped);
setCurrentProgress(0);
await preparePlayerForPath();
}
}
});
Expand Down

0 comments on commit 91f9865

Please sign in to comment.