1
請向我展示控制媒體播放器(來自服務器的流媒體MP3)的代碼。我知道這很簡單,但我不知道從哪裏開始。我實際上並不知道更多關於mediaController
,mediaSessionCompat
和NotifacationCompat.MediaStyle
。如何通過通知和鎖定屏幕控制音樂播放服務
請參考代碼做說。我只想播放/暫停和關閉圖標的圖標。
非常感謝您提前。
請向我展示控制媒體播放器(來自服務器的流媒體MP3)的代碼。我知道這很簡單,但我不知道從哪裏開始。我實際上並不知道更多關於mediaController
,mediaSessionCompat
和NotifacationCompat.MediaStyle
。如何通過通知和鎖定屏幕控制音樂播放服務
請參考代碼做說。我只想播放/暫停和關閉圖標的圖標。
非常感謝您提前。
您需要的第一件事是註冊一個媒體會話並設置一個回調以接收來自鎖定屏幕的事件。
mSession = new MediaSession(this, "MusicService");
mSession.setCallback(new MediaSessionCallback());
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
創建使用addAction
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;
}
這裏通知
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()
添加動作是一個教程的Android Music Player Controls on Lock Screen and Notifications