Browse Source

上传代码

weicu
liwei1dao 1 year ago
parent
commit
9110e443f8
  1. 1
      local_plugins/music_service/android/src/main/AndroidManifest.xml
  2. 41
      local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt
  3. 2
      local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicServiceStarter.kt

1
local_plugins/music_service/android/src/main/AndroidManifest.xml

@ -3,6 +3,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application>
<service

41
local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt

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

2
local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicServiceStarter.kt

@ -2,8 +2,10 @@ package com.yunqiinnovation.music_service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.content.ContextCompat
import java.util.jar.Manifest
//@Parcelize
//data class Song(

Loading…
Cancel
Save