From fa622af528cada193cccd386f3029b5b56b61b4a Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Fri, 23 May 2025 16:00:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../android/src/main/AndroidManifest.xml | 1 + .../music_service/MusicService.kt | 41 ++++++++++++++----- .../music_service/MusicServiceStarter.kt | 2 + 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/local_plugins/music_service/android/src/main/AndroidManifest.xml b/local_plugins/music_service/android/src/main/AndroidManifest.xml index 0b1f1fde9..3bb95fe3e 100644 --- a/local_plugins/music_service/android/src/main/AndroidManifest.xml +++ b/local_plugins/music_service/android/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ + = 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, "继续播放") } } diff --git a/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicServiceStarter.kt b/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicServiceStarter.kt index 4c743d18a..b46fa3230 100644 --- a/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicServiceStarter.kt +++ b/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(