@@ -20,6 +20,7 @@ private const val RECORD_AUDIO_REQUEST_CODE = 1001
2020class 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
0 commit comments