Skip to content

Commit 8d4f2b3

Browse files
fix: How to stop recorder when app is killed (#521) (#654)
1 parent a024184 commit 8d4f2b3

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

android/src/main/java/com/dooboolab.audiorecorderplayer/RNAudioRecorderPlayerModule.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import java.io.IOException
1818
import java.util.*
1919
import kotlin.math.log10
2020

21-
class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), PermissionListener {
21+
class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), PermissionListener, LifecycleEventListener {
2222
private var audioFileURL = ""
2323
private var subsDurationMillis = 500
2424
private var _meteringEnabled = false
@@ -413,6 +413,35 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont
413413
return false
414414
}
415415

416+
init {
417+
reactContext.addLifecycleEventListener(this)
418+
}
419+
420+
override fun onHostDestroy() {
421+
autoSaveRecordingIfNeeded()
422+
}
423+
424+
override fun onHostPause() {}
425+
426+
override fun onHostResume() {}
427+
428+
private fun autoSaveRecordingIfNeeded() {
429+
if (mediaRecorder != null) {
430+
try {
431+
mediaRecorder!!.stop()
432+
mediaRecorder!!.release()
433+
mediaRecorder = null
434+
Log.d(tag, "Recording auto-saved on app pause/destroy.")
435+
} catch (e: Exception) {
436+
Log.e(tag, "Error auto-saving recording: ${e.message}")
437+
}
438+
}
439+
}
440+
441+
protected fun finalize() {
442+
reactContext.removeLifecycleEventListener(this)
443+
}
444+
416445
companion object {
417446
private var tag = "RNAudioRecorderPlayer"
418447
private var defaultFileName = "sound.mp4"

ios/RNAudioRecorderPlayer.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
2929
override init() {
3030
super.init()
3131
NotificationCenter.default.addObserver(self, selector: #selector(handleAudioSessionInterruption(_:)), name: AVAudioSession.interruptionNotification, object: AVAudioSession.sharedInstance())
32+
NotificationCenter.default.addObserver(self, selector: #selector(handleAppWillTerminate), name: UIApplication.willTerminateNotification, object: nil)
3233
}
3334

3435
deinit {
@@ -568,4 +569,18 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
568569
return "audio"
569570
}
570571
}
572+
573+
@objc
574+
func handleAppWillTerminate() {
575+
autoSaveRecordingIfNeeded()
576+
}
577+
578+
private func autoSaveRecordingIfNeeded() {
579+
if let recorder = self.audioRecorder, recorder.isRecording {
580+
recorder.stop()
581+
recordTimer?.invalidate()
582+
recordTimer = nil
583+
// Optionally send an event or handle post-save logic here
584+
}
585+
}
571586
}

0 commit comments

Comments
 (0)