gpt4 book ai didi

android - 如何通过通知和锁屏控制服务中的音乐播放

转载 作者:可可西里 更新时间:2023-11-01 11:42:51 25 4
gpt4 key购买 nike

请告诉我从通知和锁定屏幕控制媒体播放器(从服务器流式传输 mp3)的代码。我知道这很简单,但我不知道从哪里开始。我实际上对 mediaControllermediaSessionCompatNotifacationCompat.MediaStyle 了解不多。

请引用代码做说。我只想播放/暂停和关闭图标的图标。

非常非常感谢您。

最佳答案

您需要做的第一件事是注册媒体 session 并设置回调以从锁定屏幕接收事件。

mSession = new MediaSession(this, "MusicService");
mSession.setCallback(new MediaSessionCallback());
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

创建通知

Notification.Builder notificationBuilder = new Notification.Builder(mService);
notificationBuilder
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(new int[]{playPauseButtonPosition}) // show only play/pause in compact view
.setMediaSession(mSessionToken))
.setColor(mNotificationColor)
.setSmallIcon(R.drawable.ic_notification)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setUsesChronometer(true)
.setContentIntent(createContentIntent(description)) // Create an intent that would open the UI when user clicks the notification
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setLargeIcon(art).build()

使用 addAction 添加 Action

Notification notification = createNotification();
if (notification != null) {
mController.registerCallback(mCb);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_NEXT);
filter.addAction(ACTION_PAUSE);
filter.addAction(ACTION_PLAY);
filter.addAction(ACTION_PREV);
mService.registerReceiver(this, filter);

mService.startForeground(NOTIFICATION_ID, notification);
mStarted = true;
}

这是关于 Android Music Player Controls on Lock Screen and Notifications 的教程

关于android - 如何通过通知和锁屏控制服务中的音乐播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com