From 7376b902e902da8bf0edac8d6a0fc249ed215a14 Mon Sep 17 00:00:00 2001 From: fengge <844143714@qq.com> Date: Fri, 24 Jul 2026 12:02:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A3=B0=E9=9F=B3=E6=92=AD=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/stand/standapp/AndroidInterface.kt | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/stand/standapp/AndroidInterface.kt b/app/src/main/java/com/stand/standapp/AndroidInterface.kt index 4e5aa70..f881952 100644 --- a/app/src/main/java/com/stand/standapp/AndroidInterface.kt +++ b/app/src/main/java/com/stand/standapp/AndroidInterface.kt @@ -537,7 +537,7 @@ class AndroidInterface(private val activity: MainActivity) { fun playAlert(count: Int, volume: Float) { Handler(Looper.getMainLooper()).post { try { - stopAlert() + realStopAlert() alertPlayCount = 0 alertPlayer = android.media.MediaPlayer.create(activity, R.raw.pull_alert).apply { setVolume(volume, volume) @@ -549,10 +549,10 @@ class AndroidInterface(private val activity: MainActivity) { it.start() } catch (e: Exception) { Timber.tag("AlertPlayer").e(e, "Error repeating alert play") - stopAlert() + realStopAlert() } } else { - stopAlert() + realStopAlert() } } start() @@ -565,19 +565,27 @@ class AndroidInterface(private val activity: MainActivity) { } 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 + if (Looper.myLooper() == Looper.getMainLooper()) { + realStopAlert() + } else { + Handler(Looper.getMainLooper()).post { + realStopAlert() } } } + + private fun realStopAlert() { + 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 + } + } }