gpt4 book ai didi

android - MediaStyle 通知未响应 RemoteControl 事件。

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:57 25 4
gpt4 key购买 nike

我们正在将正在进行的播放通知迁移到 Lollipop 中引入的 MediaStyle 通知。 RemoteControlClient 似乎已被弃用,并且 MediaStyle 通知未处理媒体按钮事件(例如通过耳机 Remote 暂停/播放)。

有没有人得到这个工作?没有调用 MediaSessionCallback 中的任何事件。

这里是媒体 session 是如何初始化的

    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);

关于android - MediaStyle 通知未响应 RemoteControl 事件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26496933/

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