我正在尝试创建一个 Podcast 播放器。所以遵循Android Universal Media Player源代码。我面临的问题是,我不提前知道我的播放列表,而播放列表的创建是在用户交互的基础上进行的。所以我需要从我的活动/片段中添加/删除媒体项目。我的代码与 UMP 示例应用程序非常相似,我还修改了 MusicService 的 mediaSession
从
// Create a new MediaSession.
mediaSession = MediaSessionCompat(this, "MusicService")
.apply {
setSessionActivity(sessionActivityPendingIntent)
isActive = true
}
至
// Create a new MediaSession.
mediaSession = MediaSessionCompat(this, "MusicService")
.apply {
setSessionActivity(sessionActivityPendingIntent)
isActive = true
setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
or MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS
or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
}
我正在尝试将项目添加到队列中,如下所示
fun addRandomMedia() {
if (mediaSessionConnection.isConnected.value == true) {
val mediaDescription = MediaDescriptionCompat.Builder()
.setMediaId("wake_up_01")
.setTitle("Intro - The Way Of Waking Up (feat. Alan Watts)")
.setMediaUri(Uri.parse("https://storage.googleapis.com/uamp/The_Kyoto_Connection_-_Wake_Up/01_-_Intro_-_The_Way_Of_Waking_Up_feat_Alan_Watts.mp3"))
.build()
mediaSessionConnection.mediaController.addQueueItem(mediaDescription)
}
}
但我总是得到这个例外
Process: com.example.android.uamp.next, PID: 8446
java.lang.UnsupportedOperationException: This session doesn't support queue management operations
at android.support.v4.media.session.MediaControllerCompat$MediaControllerImplApi21.addQueueItem(MediaControllerCompat.java:1988)
at android.support.v4.media.session.MediaControllerCompat.addQueueItem(MediaControllerCompat.java:316)
at com.example.android.uamp.viewmodels.MainActivityViewModel.addItemToQueue(MainActivityViewModel.kt:118)
at com.example.android.uamp.MainActivity$onCreate$3.onClick(MainActivity.kt:68)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这里是 addQueueItem 代码表单支持 v4 媒体包
@Override
public void addQueueItem(MediaDescriptionCompat description) {
long flags = getFlags();
if ((flags & MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS) == 0) {
throw new UnsupportedOperationException(
"This session doesn't support queue management operations");
}
Bundle params = new Bundle();
params.putParcelable(COMMAND_ARGUMENT_MEDIA_DESCRIPTION, description);
sendCommand(COMMAND_ADD_QUEUE_ITEM, params, null);
}
所以,我觉得 getFlags 方法一定有问题,或者我在这里遗漏了一些东西,因为 if 检查总是正确的。
为了重现这个问题,我已经分叉了示例并在应用程序中添加了上述代码。这是完整的代码链接 这是重现的代码