问题描述
我们在迁移我们持续播放通知MediaStyle在棒棒堂推出通知的过程。 RemoteControlClient似乎是德precated,并且MediaStyle通知不处理媒体按钮事件(如暂停/通过耳机播放远程)。
有没有人得到这个工作?没有在MediaSessionCallback事件被调用。
下面是如何在媒体会话初始化
mSession =新MediaSessionCompat(这一点,TAG);
mSession.setCallback(新MediaSessionCallback());
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
mSession.setActive(真正的);
下面是如何元数据设置
MediaMetadataCompat.Builder metadataBuilder =新MediaMetadataCompat.Builder();
metadataBuilder
.putLong(MediaMetadata.METADATA_KEY_DURATION,clip.getDuration())
.putString(MediaMetadata.METADATA_KEY_MEDIA_ID,clip.getClipId())
.putString(MediaMetadata.METADATA_KEY_TITLE,clip.getTitle())
.putString(MediaMetadata.METADATA_KEY_ARTIST,clip.getSourceName())
.putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI,clip.getImageUrl())
.putLong(MediaMetadata.METADATA_KEY_DURATION,clip.getDuration());
mSession.setMetadata(metadataBuilder.build());
最后,通知code:
MediaSession mediaSession =(MediaSession)session.getMediaSession();
Notification.Builder建设者=
新Notification.Builder(C)
.setDefaults(0)
.setSmallIcon(R.drawable.ic_notif)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(clip.getTitle())
.setContentText(clip.getSourceName())
.setProgress((INT)持续时间(INT)的进步,FALSE)
.setWhen(0)
.setContentIntent(pendingIntent);
如果(打){
builder.addAction(R.drawable.ic_media_pause,c.getString(R.string.media_pause)
getPendingIntentForKey code(app.getApplicationContext(),KeyEvent.KEY code_MEDIA_PAUSE));
} 其他 {
builder.addAction(R.drawable.ic_media_play,c.getString(R.string.media_play)
getPendingIntentForKey code(app.getApplicationContext(),KeyEvent.KEY code_MEDIA_PLAY));
}
builder.addAction(R.drawable.ic_media_next,c.getString(R.string.media_next)
getPendingIntentForKey code(app.getApplicationContext(),KeyEvent.KEY code_MEDIA_NEXT));
builder.setStyle(新Notification.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(新INT [] {1,2})
)
);
通知= builder.build();
设置在MediaSession与行动的播放状态,你支持:
PlaybackState状态=新PlaybackState.Builder()
.setActions(
PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE |
PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE |
PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_ preVIOUS)
.setState(PlaybackState.STATE_PLAYING,位置,速度,SystemClock.elapsedRealtime())
。建立();
mSession.setPlaybackState(州);
We are in the process of migrating our ongoing playback notification to MediaStyle notifications introduced in Lollipop. RemoteControlClient seems to be deprecated, and the MediaStyle notification is not handling the media button events (such as pause/play through headphones remote).
Did anyone get this work? None of the events in MediaSessionCallback are called.
Here is how the media session is initialized
mSession = new MediaSessionCompat(this, TAG);
mSession.setCallback(new MediaSessionCallback());
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
mSession.setActive(true);
Here is how metadata is is set
MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();
metadataBuilder
.putLong(MediaMetadata.METADATA_KEY_DURATION, clip.getDuration())
.putString(MediaMetadata.METADATA_KEY_MEDIA_ID, clip.getClipId())
.putString(MediaMetadata.METADATA_KEY_TITLE, clip.getTitle())
.putString(MediaMetadata.METADATA_KEY_ARTIST, clip.getSourceName())
.putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, clip.getImageUrl())
.putLong(MediaMetadata.METADATA_KEY_DURATION, clip.getDuration());
mSession.setMetadata(metadataBuilder.build());
Finally, the notification code:
MediaSession mediaSession = (MediaSession) session.getMediaSession();
Notification.Builder builder =
new Notification.Builder(c)
.setDefaults(0)
.setSmallIcon(R.drawable.ic_notif)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(clip.getTitle())
.setContentText(clip.getSourceName())
.setProgress((int)duration, (int)progress, false)
.setWhen(0)
.setContentIntent(pendingIntent);
if (playing) {
builder.addAction(R.drawable.ic_media_pause, c.getString(R.string.media_pause),
getPendingIntentForKeyCode(app.getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PAUSE));
} else {
builder.addAction(R.drawable.ic_media_play, c.getString(R.string.media_play),
getPendingIntentForKeyCode(app.getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PLAY));
}
builder.addAction(R.drawable.ic_media_next, c.getString(R.string.media_next),
getPendingIntentForKeyCode(app.getApplicationContext(), KeyEvent.KEYCODE_MEDIA_NEXT));
builder.setStyle(new Notification.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(new int[] {1, 2})
)
);
notification = builder.build();
Set the playback state in your MediaSession with the actions that you support:
PlaybackState state = new PlaybackState.Builder()
.setActions(
PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE |
PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE |
PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
.setState(PlaybackState.STATE_PLAYING, position, speed, SystemClock.elapsedRealtime())
.build();
mSession.setPlaybackState(state);
这篇关于MediaStyle通知没有响应REMOTECONTROL事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!