Skip to content

Commit

Permalink
fix: handle IllegalStateException in android
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Oct 31, 2023
1 parent eba5d86 commit c5ef0f1
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -294,15 +297,15 @@ 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)
}
}

@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
}

Expand All @@ -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
}

Expand Down

0 comments on commit c5ef0f1

Please sign in to comment.