Skip to content

Commit 66e50e2

Browse files
fix(UNT-T28813): runtime exception on recording in android
1 parent 7af4dcf commit 66e50e2

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

android/src/main/java/com/audiowaveform/AudioRecorder.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private const val RECORD_AUDIO_REQUEST_CODE = 1001
2020
class AudioRecorder {
2121
private var permissions = arrayOf(Manifest.permission.RECORD_AUDIO)
2222
private var useLegacyNormalization = false
23+
private var isRecording = false
2324

2425
private fun isPermissionGranted(activity: Activity?): Int? {
2526
return activity?.let { ActivityCompat.checkSelfPermission(it, permissions[0]) }
@@ -122,18 +123,26 @@ class AudioRecorder {
122123

123124
fun stopRecording(recorder: MediaRecorder?, path: String, promise: Promise) {
124125
try {
125-
recorder?.apply {
126-
stop()
127-
reset()
128-
release()
126+
if (isRecording) {
127+
recorder?.apply {
128+
stop()
129+
reset()
130+
release()
131+
}
132+
isRecording = false
133+
val tempArrayForCommunication : MutableList<String> = mutableListOf()
134+
val duration = getDuration(path)
135+
tempArrayForCommunication.add(path)
136+
tempArrayForCommunication.add(duration.toString())
137+
promise.resolve(Arguments.fromList(tempArrayForCommunication))
138+
} else {
139+
promise.reject("Error", "Recorder is not recording or has already been stopped")
129140
}
130-
val tempArrayForCommunication : MutableList<String> = mutableListOf()
131-
val duration = getDuration(path)
132-
tempArrayForCommunication.add(path)
133-
tempArrayForCommunication.add(duration.toString())
134-
promise.resolve(Arguments.fromList(tempArrayForCommunication))
135141
} catch (e: IllegalStateException) {
136142
Log.e(Constants.LOG_TAG, "Failed to stop recording",e)
143+
} catch (e: RuntimeException) {
144+
Log.e(Constants.LOG_TAG, "Runtime exception when stopping recording", e)
145+
promise.reject("Error", "Runtime exception: ${e.message}")
137146
}
138147
}
139148

@@ -156,10 +165,12 @@ class AudioRecorder {
156165
useLegacyNormalization = useLegacy
157166
recorder?.apply {
158167
start()
168+
isRecording = true
159169
}
160170
promise.resolve(true)
161171
} catch (e: IllegalStateException) {
162172
Log.e(Constants.LOG_TAG, "Failed to start recording")
173+
isRecording = false
163174
}
164175
}
165176

android/src/main/java/com/audiowaveform/AudioWaveformModule.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
3131
private var sampleRate: Int = 44100
3232
private var bitRate: Int = 128000
3333
private val handler = Handler(Looper.getMainLooper())
34+
private var startTime: Long = 0
3435

3536
companion object {
3637
const val NAME = "AudioWaveform"
@@ -80,6 +81,7 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
8081
initRecorder(obj, promise)
8182
val useLegacyNormalization = true
8283
audioRecorder.startRecorder(recorder, useLegacyNormalization, promise)
84+
startTime = System.currentTimeMillis() // Initialize startTime
8385
startEmittingRecorderValue()
8486
}
8587

@@ -104,10 +106,21 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
104106
return
105107
}
106108

107-
audioRecorder.stopRecording(recorder, path!!, promise)
108-
stopEmittingRecorderValue()
109-
recorder = null
110-
path = null
109+
try {
110+
val currentTime = System.currentTimeMillis()
111+
if (currentTime - startTime < 500) {
112+
promise.reject("SHORT_RECORDING", "Recording is too short")
113+
return
114+
}
115+
116+
stopEmittingRecorderValue()
117+
audioRecorder.stopRecording(recorder, path!!, promise)
118+
recorder = null
119+
path = null
120+
} catch (e: Exception) {
121+
Log.e(Constants.LOG_TAG, "Failed to stop recording", e)
122+
promise.reject("Error", "Failed to stop recording: ${e.message}")
123+
}
111124
}
112125

113126
@ReactMethod

0 commit comments

Comments
 (0)