From 3dad636d7e523f9bcb36479a5ca371e4f61deb39 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Mon, 12 Jan 2026 21:00:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(music=5Fservice):=20=E6=B7=BB=E5=8A=A0compl?= =?UTF-8?q?eteStop=E6=96=B9=E6=B3=95=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将分散的停止逻辑统一到completeStop方法中,避免资源释放遗漏 修复DELETE_ACTION未正确处理的问题 --- .../music_service/MusicService.kt | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt b/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt index 01248f8b1..0bda0f3e1 100644 --- a/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt +++ b/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt @@ -196,6 +196,12 @@ private fun updateProgress() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Log.i(TAG, "onStartCommand: $intent") + intent?.action?.let { + if (it == DELETE_ACTION) { + completeStop() + return START_NOT_STICKY + } + } intent?.action?.takeIf { it == Intent.ACTION_MEDIA_BUTTON }?.let { // 分发给 MediaSession 处理媒体按钮 MediaButtonReceiver.handleIntent(mediaSession, intent) @@ -235,7 +241,7 @@ private fun updateProgress() { } "pause" -> pause() "resume" -> resume() - "stop" -> stopSelf() + "stop" -> completeStop() "next" -> if (currentIndex + 1 < playlist.size) play(playlist[++currentIndex]) "previous" -> if (currentIndex - 1 >= 0) play(playlist[--currentIndex]) "seekTo" -> { @@ -267,7 +273,7 @@ private fun updateProgress() { } "com.yunqiinnovation.music_service.MEDIA_CLOSE" -> { Log.i(TAG, "收到 MEDIA_CLOSE 广播") - stopSelf() + completeStop() } "com.yunqiinnovation.music_service.MEDIA_PREVIOUS" -> { Log.i(TAG, "收到 MEDIA_PREVIOUS 广播") @@ -282,7 +288,7 @@ private fun updateProgress() { reduceVolume() } "com.yunqiinnovation.music_service.AI_SAY_END" -> { - Log.i(TAG, "收到 AI_SAY_START 广播 降低播放音量") + Log.i(TAG, "收到 AI_SAY_END 广播 恢复播放音量") restoreVolume() } // 你也可以扩展支持更多指令 @@ -330,9 +336,7 @@ private fun updateProgress() { } override fun onStop() { Log.i(TAG, "onStop 通知!") - stopSelf() - // 通知Flutter层 - flutterChannel?.invokeMethod("onMediaSessionStop", null) + completeStop() } }) // 初始播放状态 @@ -653,7 +657,7 @@ private fun updateProgress() { } private fun playNextOrStop() { if (currentIndex + 1 < playlist.size) play(playlist[++currentIndex]) - else stopSelf() + else completeStop() } private fun seekTo(positionMs: Int) { @@ -671,6 +675,32 @@ private fun updateProgress() { } } + private fun completeStop() { + try { + progressHandler.removeCallbacks(progressUpdater) + mediaPlayer?.apply { + try { + if (isPlaying) stop() + } catch (_: Exception) {} + try { + release() + } catch (_: Exception) {} + } + mediaPlayer = null + currentStatus = false + setPlaybackState(PlaybackStateCompat.STATE_STOPPED) + val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + manager.cancel(NOTIFICATION_ID) + stopForeground(true) + Log.i(TAG, "已停止并释放资源") + } catch (e: Exception) { + Log.e(TAG, "停止播放失败: ${e.message}") + } finally { + flutterChannel?.invokeMethod("onMediaSessionStop", null) + stopSelf() + } + } + private fun updateNotification(title: String,artist: String,coverUrl: String?) { val notification = createNotification(title, artist, coverUrl) val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager @@ -705,4 +735,3 @@ private fun updateProgress() { override fun onBind(intent: Intent?): IBinder? = null } -