diff --git a/android/src/main/java/com/audiowaveform/AudioPlayer.kt b/android/src/main/java/com/audiowaveform/AudioPlayer.kt index 51e44ee..bca8d98 100644 --- a/android/src/main/java/com/audiowaveform/AudioPlayer.kt +++ b/android/src/main/java/com/audiowaveform/AudioPlayer.kt @@ -145,7 +145,7 @@ class AudioPlayer( } } - fun stop(promise: Promise) { + fun stop() { stopListening() if (playerListener != null) { player.removeListener(playerListener!!) @@ -153,7 +153,6 @@ class AudioPlayer( isPlayerPrepared = false player.stop() player.release() - promise.resolve(true) } fun pause(promise: Promise) { @@ -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) { diff --git a/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt b/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt index b3ca9a5..6565958 100644 --- a/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt +++ b/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt @@ -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") } @@ -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()) } } @@ -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()) } } diff --git a/ios/AudioPlayer.swift b/ios/AudioPlayer.swift index 7e6ca22..4f0e48b 100644 --- a/ios/AudioPlayer.swift +++ b/ios/AudioPlayer.swift @@ -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) { @@ -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 } } diff --git a/ios/AudioWaveform.swift b/ios/AudioWaveform.swift index 8928b83..db3b161 100644 --- a/ios/AudioWaveform.swift +++ b/ios/AudioWaveform.swift @@ -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()) } @@ -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) @@ -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()) } diff --git a/src/components/Waveform/Waveform.tsx b/src/components/Waveform/Waveform.tsx index 5556bb2..d156df5 100644 --- a/src/components/Waveform/Waveform.tsx +++ b/src/components/Waveform/Waveform.tsx @@ -55,7 +55,7 @@ export const Waveform = forwardRef((props, ref) => { onPlayerStateChange, onRecorderStateChange, onPanStateChange = () => {}, - onError, + onError = () => {}, onCurrentProgressChange = () => {}, candleHeightScale = 3, onChangeWaveformLoadState, @@ -448,7 +448,6 @@ export const Waveform = forwardRef((props, ref) => { if (data.finishType === FinishMode.stop) { setPlayerState(PlayerState.stopped); setCurrentProgress(0); - await preparePlayerForPath(); } } });