From c5ef0f1fe51697d5ca11254ffc182c2b099b96af Mon Sep 17 00:00:00 2001 From: hyochan Date: Tue, 31 Oct 2023 21:10:42 +0900 Subject: [PATCH] fix: handle IllegalStateException in android --- .../RNAudioRecorderPlayerModule.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/dooboolab.audiorecorderplayer/RNAudioRecorderPlayerModule.kt b/android/src/main/java/com/dooboolab.audiorecorderplayer/RNAudioRecorderPlayerModule.kt index d0ee567f..bbfb4f10 100644 --- a/android/src/main/java/com/dooboolab.audiorecorderplayer/RNAudioRecorderPlayerModule.kt +++ b/android/src/main/java/com/dooboolab.audiorecorderplayer/RNAudioRecorderPlayerModule.kt @@ -224,22 +224,25 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont } mediaPlayer!!.setOnPreparedListener { mp -> - Log.d(tag, "mediaplayer prepared and start") + Log.d(tag, "Mediaplayer prepared and start") mp.start() /** * Set timer task to send event to RN. */ mTask = object : TimerTask() { override fun run() { - if (mp.isPlaying) { + try { val obj = Arguments.createMap() obj.putInt("duration", mp.duration) obj.putInt("currentPosition", mp.currentPosition) sendEvent(reactContext, "rn-playback", obj) + } catch (e: IllegalStateException) { + // IllegalStateException 처리 + Log.e(tag, "Mediaplayer error: ${e.message}") } } } - + mTimer = Timer() mTimer!!.schedule(mTask, 0, subsDurationMillis.toLong()) val resolvedPath = if (((path == "DEFAULT"))) "${reactContext.cacheDir}/$defaultFileName" else path @@ -280,12 +283,12 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont @ReactMethod fun resumePlayer(promise: Promise) { if (mediaPlayer == null) { - promise.reject("resume", "mediaPlayer is null on resume.") + promise.reject("resume", "Mediaplayer is null on resume.") return } if (mediaPlayer!!.isPlaying) { - promise.reject("resume", "mediaPlayer is already running.") + promise.reject("resume", "Mediaplayer is already running.") return } @@ -294,7 +297,7 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont mediaPlayer!!.start() promise.resolve("resume player") } catch (e: Exception) { - Log.e(tag, "mediaPlayer resume: " + e.message) + Log.e(tag, "Mediaplayer resume: " + e.message) promise.reject("resume", e.message) } } @@ -302,7 +305,7 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont @ReactMethod fun pausePlayer(promise: Promise) { if (mediaPlayer == null) { - promise.reject("pausePlay", "mediaPlayer is null on pause.") + promise.reject("pausePlay", "Mediaplayer is null on pause.") return } @@ -318,7 +321,7 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont @ReactMethod fun seekToPlayer(time: Double, promise: Promise) { if (mediaPlayer == null) { - promise.reject("seekTo", "mediaPlayer is null on seek.") + promise.reject("seekTo", "Mediaplayer is null on seek.") return }