|
|
|
@ -4,8 +4,10 @@ import android.app.Notification |
|
|
|
import android.app.NotificationChannel |
|
|
|
import android.app.NotificationManager |
|
|
|
import android.app.PendingIntent |
|
|
|
import android.content.BroadcastReceiver |
|
|
|
import android.content.Context |
|
|
|
import android.content.Intent |
|
|
|
import android.content.IntentFilter |
|
|
|
import android.graphics.BitmapFactory |
|
|
|
import android.media.AudioManager |
|
|
|
import android.media.MediaPlayer |
|
|
|
@ -41,7 +43,7 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
|
fun isPlaying() = currentStatus |
|
|
|
fun getCurrentUrl(): String? = currentUrlInternal |
|
|
|
} |
|
|
|
|
|
|
|
private var isReceiverRegistered = false |
|
|
|
private lateinit var mediaSession: MediaSessionCompat |
|
|
|
private lateinit var audioManager: AudioManager |
|
|
|
private var mediaPlayer: MediaPlayer? = null |
|
|
|
@ -52,6 +54,22 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
|
setupMediaSession() |
|
|
|
startForeground(NOTIFICATION_ID, createNotification("等待播放","DeepSound","")) |
|
|
|
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager |
|
|
|
|
|
|
|
// 注册广播监听器 |
|
|
|
val filter = IntentFilter().apply { |
|
|
|
addAction("com.yunqiinnovation.music_service.MEDIA_PLAY") |
|
|
|
addAction("com.yunqiinnovation.music_service.MEDIA_PAUSE") |
|
|
|
addAction("com.yunqiinnovation.music_service.MEDIA_PREVIOUS") |
|
|
|
addAction("com.yunqiinnovation.music_service.MEDIA_NEXT") |
|
|
|
} |
|
|
|
|
|
|
|
// Android 14+ 需要指定 flag |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
|
|
|
registerReceiver(controlReceiver, filter, Context.RECEIVER_NOT_EXPORTED) |
|
|
|
} else { |
|
|
|
registerReceiver(controlReceiver, filter) |
|
|
|
} |
|
|
|
isReceiverRegistered = true |
|
|
|
Log.i(TAG, "MediaService 启动完成") |
|
|
|
} |
|
|
|
|
|
|
|
@ -100,6 +118,32 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
|
return START_STICKY |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private val controlReceiver = object : BroadcastReceiver() { |
|
|
|
override fun onReceive(context: Context?, intent: Intent?) { |
|
|
|
Log.i(TAG, "接受到通知事件 ${intent?.action}") |
|
|
|
when (intent?.action) { |
|
|
|
"com.yunqiinnovation.music_service.MEDIA_PAUSE" -> { |
|
|
|
Log.i(TAG, "收到 MEDIA_PAUSE 广播") |
|
|
|
pause() |
|
|
|
} |
|
|
|
"com.yunqiinnovation.music_service.MEDIA_PLAY" -> { |
|
|
|
Log.i(TAG, "收到 MEDIA_RESUME 广播") |
|
|
|
resume() |
|
|
|
} |
|
|
|
"com.yunqiinnovation.music_service.MEDIA_PREVIOUS" -> { |
|
|
|
Log.i(TAG, "收到 MEDIA_RESUME 广播") |
|
|
|
playPrevious() |
|
|
|
} |
|
|
|
"com.yunqiinnovation.music_service.MEDIA_NEXT" -> { |
|
|
|
Log.i(TAG, "收到 MEDIA_RESUME 广播") |
|
|
|
playNext() |
|
|
|
} |
|
|
|
// 你也可以扩展支持更多指令 |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private fun setupMediaSession() { |
|
|
|
mediaSession = MediaSessionCompat(this, "MusicMediaSession").apply { |
|
|
|
setFlags( |
|
|
|
@ -190,19 +234,33 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
|
|
|
|
|
return builder.build() |
|
|
|
} |
|
|
|
|
|
|
|
fun mediaButtonPendingIntent(action: Long): PendingIntent { |
|
|
|
val intent = Intent(this, MusicService::class.java).apply { |
|
|
|
this.action = Intent.ACTION_MEDIA_BUTTON |
|
|
|
putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, action.toKeyCode())) |
|
|
|
val keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, action.toKeyCode()) |
|
|
|
val intent = Intent(Intent.ACTION_MEDIA_BUTTON).apply { |
|
|
|
setPackage(packageName) |
|
|
|
putExtra(Intent.EXTRA_KEY_EVENT, keyEvent) |
|
|
|
} |
|
|
|
|
|
|
|
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE |
|
|
|
} else { |
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT |
|
|
|
} |
|
|
|
return PendingIntent.getService(this, action.toInt(), intent, flags) |
|
|
|
|
|
|
|
return PendingIntent.getBroadcast(this, action.toInt(), intent, flags) |
|
|
|
} |
|
|
|
// fun mediaButtonPendingIntent(action: Long): PendingIntent { |
|
|
|
// val intent = Intent(this, MusicService::class.java).apply { |
|
|
|
// this.action = Intent.ACTION_MEDIA_BUTTON |
|
|
|
// putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, action.toKeyCode())) |
|
|
|
// } |
|
|
|
// val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
// PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE |
|
|
|
// } else { |
|
|
|
// PendingIntent.FLAG_UPDATE_CURRENT |
|
|
|
// } |
|
|
|
// return PendingIntent.getService(this, action.toInt(), intent, flags) |
|
|
|
// } |
|
|
|
// 转换 PlaybackStateCompat.ACTION 到 KeyEvent |
|
|
|
fun Long.toKeyCode(): Int = when (this) { |
|
|
|
PlaybackStateCompat.ACTION_PLAY -> KeyEvent.KEYCODE_MEDIA_PLAY |
|
|
|
@ -339,6 +397,10 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
|
) = result.sendResult(mutableListOf()) |
|
|
|
|
|
|
|
override fun onDestroy() { |
|
|
|
if (isReceiverRegistered) { |
|
|
|
unregisterReceiver(controlReceiver) |
|
|
|
isReceiverRegistered = false |
|
|
|
} |
|
|
|
mediaPlayer?.release() |
|
|
|
stopForeground(true) |
|
|
|
isRunning.set(false) |
|
|
|
|