|
|
@ -3,6 +3,7 @@ package com.yunqiinnovation.music_service |
|
|
import android.app.Notification |
|
|
import android.app.Notification |
|
|
import android.app.NotificationChannel |
|
|
import android.app.NotificationChannel |
|
|
import android.app.NotificationManager |
|
|
import android.app.NotificationManager |
|
|
|
|
|
import android.app.PendingIntent |
|
|
import android.content.Context |
|
|
import android.content.Context |
|
|
import android.content.Intent |
|
|
import android.content.Intent |
|
|
import android.graphics.BitmapFactory |
|
|
import android.graphics.BitmapFactory |
|
|
@ -18,6 +19,7 @@ import androidx.core.app.NotificationCompat |
|
|
import androidx.media.MediaBrowserServiceCompat |
|
|
import androidx.media.MediaBrowserServiceCompat |
|
|
import androidx.media.session.MediaButtonReceiver |
|
|
import androidx.media.session.MediaButtonReceiver |
|
|
import android.util.Log |
|
|
import android.util.Log |
|
|
|
|
|
import android.view.KeyEvent |
|
|
import java.net.HttpURLConnection |
|
|
import java.net.HttpURLConnection |
|
|
import java.net.URL |
|
|
import java.net.URL |
|
|
import java.util.concurrent.atomic.AtomicBoolean |
|
|
import java.util.concurrent.atomic.AtomicBoolean |
|
|
@ -125,6 +127,7 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
) |
|
|
) |
|
|
isActive = true |
|
|
isActive = true |
|
|
} |
|
|
} |
|
|
|
|
|
mediaSession.setMediaButtonReceiver(null) |
|
|
// 将 token 暴露给 MediaBrowserServiceCompat |
|
|
// 将 token 暴露给 MediaBrowserServiceCompat |
|
|
setSessionToken(mediaSession.sessionToken) |
|
|
setSessionToken(mediaSession.sessionToken) |
|
|
} |
|
|
} |
|
|
@ -137,15 +140,11 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
val prevIntent = MediaButtonReceiver.buildMediaButtonPendingIntent( |
|
|
val prevIntent = mediaButtonPendingIntent(PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) |
|
|
this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
|
|
val playPauseIntent = mediaButtonPendingIntent( |
|
|
) |
|
|
if (currentStatus) PlaybackStateCompat.ACTION_PAUSE else PlaybackStateCompat.ACTION_PLAY |
|
|
val playPauseIntent = MediaButtonReceiver.buildMediaButtonPendingIntent( |
|
|
|
|
|
this, if (currentStatus) PlaybackStateCompat.ACTION_PAUSE else PlaybackStateCompat.ACTION_PLAY |
|
|
|
|
|
) |
|
|
|
|
|
val nextIntent = MediaButtonReceiver.buildMediaButtonPendingIntent( |
|
|
|
|
|
this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
val nextIntent = mediaButtonPendingIntent(PlaybackStateCompat.ACTION_SKIP_TO_NEXT) |
|
|
|
|
|
|
|
|
val builder = NotificationCompat.Builder(this, CHANNEL_ID) |
|
|
val builder = NotificationCompat.Builder(this, CHANNEL_ID) |
|
|
.setContentTitle(title) |
|
|
.setContentTitle(title) |
|
|
@ -192,6 +191,26 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
return builder.build() |
|
|
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 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 |
|
|
|
|
|
PlaybackStateCompat.ACTION_PAUSE -> KeyEvent.KEYCODE_MEDIA_PAUSE |
|
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_NEXT -> KeyEvent.KEYCODE_MEDIA_NEXT |
|
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS -> KeyEvent.KEYCODE_MEDIA_PREVIOUS |
|
|
|
|
|
else -> KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE |
|
|
|
|
|
} |
|
|
/** |
|
|
/** |
|
|
* 设置播放状态 |
|
|
* 设置播放状态 |
|
|
*/ |
|
|
*/ |
|
|
@ -242,7 +261,7 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
} |
|
|
} |
|
|
prepareAsync() |
|
|
prepareAsync() |
|
|
} |
|
|
} |
|
|
setPlaybackState(PlaybackStateCompat.STATE_PAUSED); |
|
|
setPlaybackState(PlaybackStateCompat.STATE_PLAYING); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private fun pause() { |
|
|
private fun pause() { |
|
|
@ -259,7 +278,7 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
val artist = song["artist"] ?: "未知歌手" |
|
|
val artist = song["artist"] ?: "未知歌手" |
|
|
val coverUrl = song["coverUrl"] |
|
|
val coverUrl = song["coverUrl"] |
|
|
updateNotification(title,artist,coverUrl) |
|
|
updateNotification(title,artist,coverUrl) |
|
|
setPlaybackState(PlaybackStateCompat.STATE_PLAYING); |
|
|
setPlaybackState(PlaybackStateCompat.STATE_PAUSED); |
|
|
Log.i(TAG, "已暂停") |
|
|
Log.i(TAG, "已暂停") |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
@ -279,7 +298,7 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
val artist = song["artist"] ?: "未知歌手" |
|
|
val artist = song["artist"] ?: "未知歌手" |
|
|
val coverUrl = song["coverUrl"] |
|
|
val coverUrl = song["coverUrl"] |
|
|
updateNotification(title,artist,coverUrl) |
|
|
updateNotification(title,artist,coverUrl) |
|
|
setPlaybackState(PlaybackStateCompat.STATE_PAUSED); |
|
|
setPlaybackState(PlaybackStateCompat.STATE_PLAYING); |
|
|
// Log.i(TAG, "继续播放") |
|
|
// Log.i(TAG, "继续播放") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|