diff --git a/app/src/main/java/com/stand/standapp/AndroidInterface.kt b/app/src/main/java/com/stand/standapp/AndroidInterface.kt index c734d55..75b2d07 100644 --- a/app/src/main/java/com/stand/standapp/AndroidInterface.kt +++ b/app/src/main/java/com/stand/standapp/AndroidInterface.kt @@ -17,6 +17,8 @@ class AndroidInterface(private val activity: MainActivity) { private var isSpeaking = false private var lastSpeakTime = 0L private val MIN_SPEAK_DURATION = 200L + private var alertPlayer: android.media.MediaPlayer? = null + private var alertPlayCount = 0 /** * JS bridge 调度入口 @@ -88,6 +90,16 @@ class AndroidInterface(private val activity: MainActivity) { "maximizeTempCall" -> { maximizeTempCall(); "" } "getTempGroupId" -> getTempGroupId() "logout" -> { logout(); "" } + "playAlert" -> { + val count = args.optInt(0, 3) + val volume = args.optDouble(1, 1.0).toFloat() + playAlert(count, volume) + "" + } + "stopAlert" -> { + stopAlert() + "" + } else -> { Timber.tag("Bridge").w("Unknown method: $method") "" @@ -518,4 +530,50 @@ class AndroidInterface(private val activity: MainActivity) { fun logout() { activity.logout() } + + fun playAlert(count: Int, volume: Float) { + Handler(Looper.getMainLooper()).post { + try { + stopAlert() + alertPlayCount = 0 + alertPlayer = android.media.MediaPlayer.create(activity, R.raw.pull_alert).apply { + setVolume(volume, volume) + setOnCompletionListener { + alertPlayCount++ + if (alertPlayCount < count) { + try { + it.start() + } catch (e: Exception) { + Timber.tag("AlertPlayer").e(e, "Error repeating alert play") + stopAlert() + } + } else { + stopAlert() + } + } + start() + } + Timber.tag("AlertPlayer").d("Started alert play, count=$count, volume=$volume") + } catch (e: Exception) { + Timber.tag("AlertPlayer").e(e, "Error initializing alert player") + } + } + } + + fun stopAlert() { + Handler(Looper.getMainLooper()).post { + try { + alertPlayer?.let { player -> + if (player.isPlaying) { + player.stop() + } + player.release() + } + } catch (e: Exception) { + Timber.tag("AlertPlayer").e(e, "Error stopping alert player") + } finally { + alertPlayer = null + } + } + } }