Browse Source

Merge branch 'new_dev' of github.com:deepcloud2048/deep_voice into new_dev

weicu
lxm 9 months ago
parent
commit
117fe8ffb8
  1. 45
      local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt

45
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 { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i(TAG, "onStartCommand: $intent") 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 { intent?.action?.takeIf { it == Intent.ACTION_MEDIA_BUTTON }?.let {
// 分发给 MediaSession 处理媒体按钮 // 分发给 MediaSession 处理媒体按钮
MediaButtonReceiver.handleIntent(mediaSession, intent) MediaButtonReceiver.handleIntent(mediaSession, intent)
@ -235,7 +241,7 @@ private fun updateProgress() {
} }
"pause" -> pause() "pause" -> pause()
"resume" -> resume() "resume" -> resume()
"stop" -> stopSelf() "stop" -> completeStop()
"next" -> if (currentIndex + 1 < playlist.size) play(playlist[++currentIndex]) "next" -> if (currentIndex + 1 < playlist.size) play(playlist[++currentIndex])
"previous" -> if (currentIndex - 1 >= 0) play(playlist[--currentIndex]) "previous" -> if (currentIndex - 1 >= 0) play(playlist[--currentIndex])
"seekTo" -> { "seekTo" -> {
@ -267,7 +273,7 @@ private fun updateProgress() {
} }
"com.yunqiinnovation.music_service.MEDIA_CLOSE" -> { "com.yunqiinnovation.music_service.MEDIA_CLOSE" -> {
Log.i(TAG, "收到 MEDIA_CLOSE 广播") Log.i(TAG, "收到 MEDIA_CLOSE 广播")
stopSelf() completeStop()
} }
"com.yunqiinnovation.music_service.MEDIA_PREVIOUS" -> { "com.yunqiinnovation.music_service.MEDIA_PREVIOUS" -> {
Log.i(TAG, "收到 MEDIA_PREVIOUS 广播") Log.i(TAG, "收到 MEDIA_PREVIOUS 广播")
@ -282,7 +288,7 @@ private fun updateProgress() {
reduceVolume() reduceVolume()
} }
"com.yunqiinnovation.music_service.AI_SAY_END" -> { "com.yunqiinnovation.music_service.AI_SAY_END" -> {
Log.i(TAG, "收到 AI_SAY_START 广播 降低播放音量") Log.i(TAG, "收到 AI_SAY_END 广播 恢复播放音量")
restoreVolume() restoreVolume()
} }
// 你也可以扩展支持更多指令 // 你也可以扩展支持更多指令
@ -330,9 +336,7 @@ private fun updateProgress() {
} }
override fun onStop() { override fun onStop() {
Log.i(TAG, "onStop 通知!") Log.i(TAG, "onStop 通知!")
stopSelf() completeStop()
// 通知Flutter层
flutterChannel?.invokeMethod("onMediaSessionStop", null)
} }
}) })
// 初始播放状态 // 初始播放状态
@ -653,7 +657,7 @@ private fun updateProgress() {
} }
private fun playNextOrStop() { private fun playNextOrStop() {
if (currentIndex + 1 < playlist.size) play(playlist[++currentIndex]) if (currentIndex + 1 < playlist.size) play(playlist[++currentIndex])
else stopSelf() else completeStop()
} }
private fun seekTo(positionMs: Int) { 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?) { private fun updateNotification(title: String,artist: String,coverUrl: String?) {
val notification = createNotification(title, artist, coverUrl) val notification = createNotification(title, artist, coverUrl)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@ -705,4 +735,3 @@ private fun updateProgress() {
override fun onBind(intent: Intent?): IBinder? = null override fun onBind(intent: Intent?): IBinder? = null
} }

Loading…
Cancel
Save