Skip to content

Commit

Permalink
Merge pull request #26 from SimformSolutionsPvtLtd/fix/UNT-T24785_And…
Browse files Browse the repository at this point in the history
…roid_onDidFinishPlayingAudio_Event_Never_Fire

fix: UNT-T24785: onDidFinishPlayingAudio event never fires
  • Loading branch information
mukesh-simform authored May 9, 2024
2 parents af55657 + cb4107e commit c9eb6e9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 74 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions android/src/main/java/com/audiowaveform/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,28 @@ class AudioPlayer(
}
}
if (state == Player.STATE_ENDED) {
val args: MutableMap<String, Any?> = 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)
}
}
}
Expand Down
65 changes: 47 additions & 18 deletions android/src/main/java/com/audiowaveform/AudioRecorder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand All @@ -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")
}
Expand All @@ -103,10 +130,10 @@ class AudioRecorder {
val tempArrayForCommunication : MutableList<String> = 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)
}
}

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ DerivedData
*.ipa
*.xcuserstate
ios/.xcode.env.local
ios/link-assets-manifest.json

# Android/IntelliJ
#
Expand All @@ -31,6 +32,7 @@ local.properties
*.iml
*.hprof
.cxx/
android/link-assets-manifest.json

# node.js
#
Expand Down
25 changes: 0 additions & 25 deletions example/android/link-assets-manifest.json

This file was deleted.

25 changes: 0 additions & 25 deletions example/ios/link-assets-manifest.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/WaveformCandle/WaveformCandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit c9eb6e9

Please sign in to comment.