Android MediaStyle通知未响应远程控制事件。
我们正在将正在播放的通知迁移到棒棒糖中引入的MediaStyle通知。RemoteControlClient似乎已弃用,MediaStyle通知未处理媒体按钮事件(例如通过耳机远程暂停/播放) 有人得到这份工作吗?没有调用MediaSessionCallback中的任何事件 下面是媒体会话的初始化方式Android MediaStyle通知未响应远程控制事件。,android,android-5.0-lollipop,Android,Android 5.0 Lollipop,我们正在将正在播放的通知迁移到棒棒糖中引入的MediaStyle通知。RemoteControlClient似乎已弃用,MediaStyle通知未处理媒体按钮事件(例如通过耳机远程暂停/播放) 有人得到这份工作吗?没有调用MediaSessionCallback中的任何事件 下面是媒体会话的初始化方式 mSession = new MediaSessionCompat(this, TAG); mSession.setCallback(new MediaSessionCallbac
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);
下面是元数据的设置方式
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());
最后,通知代码:
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();
使用您支持的操作设置MediaSession中的播放状态:
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);
对于Compat库,它应该类似于:PlaybackStateCompat.Builder stateBuilder=newplaybackstatecompat.Builder();stateBuilder.setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY |暂停| PlaybackState.ACTION_PLAY |暂停| PlaybackState.ACTION |跳到下一个| PlaybackState.ACTION |跳到上一个);stateBuilder.setState(PlaybackState.STATE_PLAYING,0,1);m_objMediaSession.setPlaybackState(stateBuilder.build());^稍微更正一下,它将是
PlaybackStateCompat.ACTION\u PLAY
等