Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If startRecorder fails, the recorder is left in an invalid, unrecoverable state #625

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,47 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont
audioFileURL = if (((path == "DEFAULT"))) "${reactContext.cacheDir}/sound.${defaultFileExtensions.get(outputFormat)}" else path
_meteringEnabled = meteringEnabled

if (mediaRecorder == null) {
mediaRecorder = MediaRecorder()
if (mediaRecorder != null) {
promise.reject("InvalidState", "startRecorder has already been called.")
return
}

if (audioSet != null) {
mediaRecorder!!.setAudioSource(if (audioSet.hasKey("AudioSourceAndroid")) audioSet.getInt("AudioSourceAndroid") else MediaRecorder.AudioSource.MIC)
mediaRecorder!!.setOutputFormat(outputFormat)
mediaRecorder!!.setAudioEncoder(if (audioSet.hasKey("AudioEncoderAndroid")) audioSet.getInt("AudioEncoderAndroid") else MediaRecorder.AudioEncoder.AAC)
mediaRecorder!!.setAudioSamplingRate(if (audioSet.hasKey("AudioSamplingRateAndroid")) audioSet.getInt("AudioSamplingRateAndroid") else 48000)
mediaRecorder!!.setAudioEncodingBitRate(if (audioSet.hasKey("AudioEncodingBitRateAndroid")) audioSet.getInt("AudioEncodingBitRateAndroid") else 128000)
mediaRecorder!!.setAudioChannels(if (audioSet.hasKey("AudioChannelsAndroid")) audioSet.getInt("AudioChannelsAndroid") else 2)
var newMediaRecorder: MediaRecorder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MediaRecorder(reactContext)
} else {
mediaRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
mediaRecorder!!.setOutputFormat(outputFormat)
mediaRecorder!!.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
mediaRecorder!!.setAudioEncodingBitRate(128000)
mediaRecorder!!.setAudioSamplingRate(48000)
MediaRecorder()
}
mediaRecorder!!.setOutputFile(audioFileURL)

try {
mediaRecorder!!.prepare()
if (audioSet == null) {
newMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
newMediaRecorder.setOutputFormat(outputFormat)
newMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
} else {
newMediaRecorder.setAudioSource(if (audioSet.hasKey("AudioSourceAndroid")) audioSet.getInt("AudioSourceAndroid") else MediaRecorder.AudioSource.MIC)
newMediaRecorder.setOutputFormat(outputFormat)
newMediaRecorder.setAudioEncoder(if (audioSet.hasKey("AudioEncoderAndroid")) audioSet.getInt("AudioEncoderAndroid") else MediaRecorder.AudioEncoder.AAC)

if (audioSet.hasKey("AudioSamplingRateAndroid")) {
newMediaRecorder.setAudioSamplingRate(audioSet.getInt("AudioSamplingRateAndroid"))
}

if (audioSet.hasKey("AudioEncodingBitRateAndroid")) {
newMediaRecorder.setAudioEncodingBitRate(audioSet.getInt("AudioEncodingBitRateAndroid"))
}

if (audioSet.hasKey("AudioChannelsAndroid")) {
newMediaRecorder.setAudioChannels(audioSet.getInt("AudioChannelsAndroid"))
}
}
newMediaRecorder.setOutputFile(audioFileURL)

newMediaRecorder.prepare()
totalPausedRecordTime = 0L
mediaRecorder!!.start()
newMediaRecorder.start()

mediaRecorder = newMediaRecorder

val systemTime = SystemClock.elapsedRealtime()
recorderRunnable = object : Runnable {
override fun run() {
Expand All @@ -118,6 +135,9 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont
(recorderRunnable as Runnable).run()
promise.resolve("file:///$audioFileURL")
} catch (e: Exception) {
newMediaRecorder.release()
mediaRecorder = null

Log.e(tag, "Exception: ", e)
promise.reject("startRecord", e.message)
}
Expand All @@ -126,7 +146,7 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont
@ReactMethod
fun resumeRecorder(promise: Promise) {
if (mediaRecorder == null) {
promise.reject("resumeReocrder", "Recorder is null.")
promise.reject("resumeRecorder", "Recorder is null.")
return
}

Expand Down Expand Up @@ -172,7 +192,6 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont

try {
mediaRecorder!!.stop()
mediaRecorder!!.reset()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbaudanza Could you kindly check why reset should be removed?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this part as remaining question and will merge this PR! Thanks @jbaudanza

mediaRecorder!!.release()
mediaRecorder = null
promise.resolve("file:///$audioFileURL")
Expand Down
15 changes: 10 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,16 @@ class AudioRecorderPlayer {
if (!this._isRecording) {
this._isRecording = true;

return RNAudioRecorderPlayer.startRecorder(
uri ?? 'DEFAULT',
audioSets,
meteringEnabled ?? false,
);
try {
return await RNAudioRecorderPlayer.startRecorder(
uri ?? 'DEFAULT',
audioSets,
meteringEnabled ?? false,
);
} catch (error: any) {
this._isRecording = false;
throw error;
}
}

return 'Already recording';
Expand Down
Loading