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