From cb4107e62f9f891072560c27d97f72e7755cf314 Mon Sep 17 00:00:00 2001 From: nilesh-simform Date: Wed, 1 May 2024 12:27:58 +0530 Subject: [PATCH] fix(UNT-T24785): event onDidFinishPlayingAudio never gets fired --- README.md | 2 + .../java/com/audiowaveform/AudioPlayer.kt | 11 ++-- .../java/com/audiowaveform/AudioRecorder.kt | 65 ++++++++++++++----- .../com/audiowaveform/AudioWaveformModule.kt | 6 ++ example/.gitignore | 2 + example/android/link-assets-manifest.json | 25 ------- example/ios/link-assets-manifest.json | 25 ------- .../WaveformCandle/WaveformCandle.tsx | 2 +- 8 files changed, 64 insertions(+), 74 deletions(-) delete mode 100644 example/android/link-assets-manifest.json delete mode 100644 example/ios/link-assets-manifest.json diff --git a/README.md b/README.md index bcdb8d1..9f102a0 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,8 @@ To use example app you need to first run below command cd example && npx react-native-asset ``` +> Note: If link-assets-manifest.json file already exists then make sure to delete that before running npx react-native-asset command. + This command will add our example audio sample files to the iOS bundle so that we can access them inside the iOS app. ```sh diff --git a/android/src/main/java/com/audiowaveform/AudioPlayer.kt b/android/src/main/java/com/audiowaveform/AudioPlayer.kt index 7a696ad..f9b5c22 100644 --- a/android/src/main/java/com/audiowaveform/AudioPlayer.kt +++ b/android/src/main/java/com/audiowaveform/AudioPlayer.kt @@ -47,27 +47,28 @@ class AudioPlayer( } } if (state == Player.STATE_ENDED) { - val args: MutableMap = HashMap() + val args: WritableMap = Arguments.createMap() when (finishMode) { FinishMode.Loop -> { player.seekTo(0) player.play() - args[Constants.finishType] = 0 + args.putInt(Constants.finishType, 0) } FinishMode.Pause -> { player.seekTo(0) player.playWhenReady = false stopListening() - args[Constants.finishType] = 1 + args.putInt(Constants.finishType, 1) } else -> { player.stop() player.release() stopListening() - args[Constants.finishType] = 2 + args.putInt(Constants.finishType, 2) } } - args[Constants.playerKey] = key + args.putString(Constants.playerKey, key) + appContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit("onDidFinishPlayingAudio", args) } } } diff --git a/android/src/main/java/com/audiowaveform/AudioRecorder.kt b/android/src/main/java/com/audiowaveform/AudioRecorder.kt index 236beb1..0dc05a6 100644 --- a/android/src/main/java/com/audiowaveform/AudioRecorder.kt +++ b/android/src/main/java/com/audiowaveform/AudioRecorder.kt @@ -54,15 +54,34 @@ class AudioRecorder { fun getDecibel(recorder: MediaRecorder?): Double? { if (useLegacyNormalization) { - val db = 20 * log10((recorder?.maxAmplitude?.toDouble() ?: (0.0 / 32768.0))) - if (db == Double.NEGATIVE_INFINITY) { - Log.e(Constants.LOG_TAG, "Microphone might be turned off") - } else { - return db + if (recorder != null) { + try { + val db = 20 * log10((recorder?.maxAmplitude?.toDouble() ?: (0.0 / 32768.0))) + if (db == Double.NEGATIVE_INFINITY) { + Log.e(Constants.LOG_TAG, "Microphone might be turned off") + } else { + return db + } + return db; + } catch (e: IllegalStateException) { + e.printStackTrace() + return null + } + } + else { + return null } - return db; } else { - return recorder?.maxAmplitude?.toDouble() ?: 0.0 + if (recorder != null) { + try { + return recorder?.maxAmplitude?.toDouble() ?: 0.0 + } catch (e: IllegalStateException) { + e.printStackTrace() + return null + } + } else { + return null + } } } @@ -75,18 +94,26 @@ class AudioRecorder { bitRate: Int?, promise: Promise ) { + if (recorder == null) { + promise.reject("RECORDER_NULL", "MediaRecorder instance is null") + return + } + recorder?.apply { - setAudioSource(MediaRecorder.AudioSource.MIC) - setOutputFormat(getOutputFormat(outputFormat)) - setAudioEncoder(getEncoder(encoder)) - setAudioSamplingRate(sampleRate) - if (bitRate != null) { - setAudioEncodingBitRate(bitRate) - } - setOutputFile(path) try { + setAudioSource(MediaRecorder.AudioSource.MIC) + setOutputFormat(getOutputFormat(outputFormat)) + setAudioEncoder(getEncoder(encoder)) + setAudioSamplingRate(sampleRate) + if (bitRate != null) { + setAudioEncodingBitRate(bitRate) + } + setOutputFile(path) prepare() promise.resolve(true) + } catch (e: IllegalArgumentException) { + Log.e(Constants.LOG_TAG, "Invalid MediaRecorder configuration", e) + promise.reject("CONFIGURATION_ERROR", "Invalid MediaRecorder configuration: ${e.message}") } catch (e: IOException) { Log.e(Constants.LOG_TAG, "Failed to stop initialize recorder") } @@ -103,10 +130,10 @@ class AudioRecorder { val tempArrayForCommunication : MutableList = mutableListOf() val duration = getDuration(path) tempArrayForCommunication.add(path) - tempArrayForCommunication.add(duration) + tempArrayForCommunication.add(duration.toString()) promise.resolve(Arguments.fromList(tempArrayForCommunication)) } catch (e: IllegalStateException) { - Log.e(Constants.LOG_TAG, "Failed to stop recording") + Log.e(Constants.LOG_TAG, "Failed to stop recording",e) } } @@ -127,7 +154,9 @@ class AudioRecorder { fun startRecorder(recorder: MediaRecorder?, useLegacy: Boolean, promise: Promise) { try { useLegacyNormalization = useLegacy - recorder?.start() + recorder?.apply { + start() + } promise.resolve(true) } catch (e: IllegalStateException) { Log.e(Constants.LOG_TAG, "Failed to start recording") diff --git a/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt b/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt index 48a5f90..fb125e5 100644 --- a/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt +++ b/android/src/main/java/com/audiowaveform/AudioWaveformModule.kt @@ -84,9 +84,15 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav @ReactMethod fun stopRecording(promise: Promise) { + if (audioRecorder == null || recorder == null || path == null) { + promise.reject("STOP_RECORDING_ERROR", "Recording resources not properly initialized") + return + } + audioRecorder.stopRecording(recorder, path!!, promise) stopEmittingRecorderValue() recorder = null + path = null } @ReactMethod diff --git a/example/.gitignore b/example/.gitignore index 8c7214e..4314a32 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -21,6 +21,7 @@ DerivedData *.ipa *.xcuserstate ios/.xcode.env.local +ios/link-assets-manifest.json # Android/IntelliJ # @@ -31,6 +32,7 @@ local.properties *.iml *.hprof .cxx/ +android/link-assets-manifest.json # node.js # diff --git a/example/android/link-assets-manifest.json b/example/android/link-assets-manifest.json deleted file mode 100644 index 65f1d77..0000000 --- a/example/android/link-assets-manifest.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "migIndex": 1, - "data": [ - { - "path": "src/assets/audio/file_example_mp3_12s.mp3", - "sha1": "27c1fa5ab09a20ef4390a207e660320015922938" - }, - { - "path": "src/assets/audio/file_example_mp3_15s.mp3", - "sha1": "7bd4d150fed7b6f6163d8b62afa87cd7ae810620" - }, - { - "path": "src/assets/audio/file_example_mp3_1mg.mp3", - "sha1": "e493d393c751d2bfa8807af52d306552b73ecae4" - }, - { - "path": "src/assets/audio/file_example_mp3_700kb.mp3", - "sha1": "dcaa8815859ce4c1c02c5f3b8bf01c79d0053c1f" - }, - { - "path": "src/assets/audio/index.ts", - "sha1": "b14b69084488672939485938fe5556e76d291b3b" - } - ] -} diff --git a/example/ios/link-assets-manifest.json b/example/ios/link-assets-manifest.json deleted file mode 100644 index 65f1d77..0000000 --- a/example/ios/link-assets-manifest.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "migIndex": 1, - "data": [ - { - "path": "src/assets/audio/file_example_mp3_12s.mp3", - "sha1": "27c1fa5ab09a20ef4390a207e660320015922938" - }, - { - "path": "src/assets/audio/file_example_mp3_15s.mp3", - "sha1": "7bd4d150fed7b6f6163d8b62afa87cd7ae810620" - }, - { - "path": "src/assets/audio/file_example_mp3_1mg.mp3", - "sha1": "e493d393c751d2bfa8807af52d306552b73ecae4" - }, - { - "path": "src/assets/audio/file_example_mp3_700kb.mp3", - "sha1": "dcaa8815859ce4c1c02c5f3b8bf01c79d0053c1f" - }, - { - "path": "src/assets/audio/index.ts", - "sha1": "b14b69084488672939485938fe5556e76d291b3b" - } - ] -} diff --git a/src/components/WaveformCandle/WaveformCandle.tsx b/src/components/WaveformCandle/WaveformCandle.tsx index 421238e..dde33c8 100644 --- a/src/components/WaveformCandle/WaveformCandle.tsx +++ b/src/components/WaveformCandle/WaveformCandle.tsx @@ -43,7 +43,7 @@ export const WaveformCandle = ({ width: candleWidth, marginRight: candleSpace, maxHeight, - height: amplitude * maxHeight * candleHeightScale, // Adjust the height scale as needed + height: (isNaN(amplitude) ? 0 : amplitude) * maxHeight * candleHeightScale, // Adjust the height scale as needed minHeight: candleWidth, borderRadius: candleWidth, },