我正在构建一个音乐播放器,目前正在尝试实现锁屏媒体控件和播放信息。我的第一次尝试是使用 RemoteControlClient,但此方法已被弃用,我遇到了无法解决的错误。所以我尝试切换到 MediaSessionCompat 类,除了媒体控件不显示外,一切正常。
ComponentName eventReceiver = new ComponentName(getPackageName(), RemoteMediaReceiver.class.getName());
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(eventReceiver);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
mediaSession = new MediaSessionCompat(this, "TAG", eventReceiver, mediaPendingIntent);
mediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
Log.d("MediaSession", "onMediaButtonEvent");
return super.onMediaButtonEvent(mediaButtonEvent);
}
@Override
public void onPlay() {
Log.d("MediaSession", "onPlay");
super.onPlay();
}
@Override
public void onPause() {
Log.d("MediaSession", "onPause");
super.onPause();
}
@Override
public void onSkipToNext() {
Log.d("MediaSession", "onSkipToNext");
super.onSkipToNext();
}
@Override
public void onSkipToPrevious() {
Log.d("MediaSession", "onSkipToPrevious");
super.onSkipToPrevious();
}
@Override
public void onSeekTo(long pos) {
Log.d("MediaSession", String.format("onSeekTo %d", pos));
super.onSeekTo(pos);
}
@Override
public void onStop() {
Log.d("MediaSession", "onStop");
super.onStop();
}
});
mediaSession.setPlaybackState(playbackState(PlaybackStateCompat.STATE_STOPPED, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 0));
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
这就是我设置媒体会话的方式,然后我有帮助方法来更新它的状态和元数据,就像这样
public void start(){
mControl.setPlaying(true);
notify(State.PLAY);
if(!audioFocus) requestFocus();
//remoteCC.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING, mp.getCurrentPosition(), 1.0f);
mediaSession.setPlaybackState(playbackState(PlaybackStateCompat.STATE_PLAYING, mp.getCurrentPosition(), 1));
mediaSession.setActive(true);
mp.start();
}
public void pause(){
mControl.setPlaying(false);
notify(State.PAUSE);
//remoteCC.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
mediaSession.setPlaybackState(playbackState(PlaybackStateCompat.STATE_PAUSED, mp.getCurrentPosition(), 0));
mediaSession.setActive(true);
mp.pause();
}
public void mediaSessionNewSong(){
Song song = mControl.getSongSet().safeCurrent();
Bitmap bitmap = AlbumArtCache.instance().get(song.album_id);
if(bitmap == null) bitmap = AlbumArtCache.instance().get(-1);
MediaMetadataCompat.Builder metadata = new MediaMetadataCompat.Builder();
metadata.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.album);
metadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artist);
metadata.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, song.track);
metadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title);
metadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration);
metadata.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap.copy(bitmap.getConfig(), false));
mediaSession.setMetadata(metadata.build());
mediaSession.setActive(true);
}
踢球者是,我可以使用 getRemoteControlClient() 手动获取 RemoteControlClient 并设置它的传输控制标志以使按钮显示,但随后它们不使用提供的回调类,它们使用 eventReceiver,我遇到了我遇到的错误使用我原来的 RemoteClientControl 实现。我没看到什么?我读过的所有文档和我看到的其他 SO 问题都表明这应该可行。
编辑:是的,我很抱歉,这是一个很大的代码库,所以我忘记了所有的辅助方法
public PlaybackStateCompat playbackState(int state, long position, float playbackSpeed){
return new PlaybackStateCompat.Builder()
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SEEK_TO)
.setState(state, position, playbackSpeed)
.build();
}
我将我的库从 22.0.0 更新到 22.2.1,现在按钮显示(进度:D),但似乎远程客户端控件使用的是 eventReceiver 而不是 MediaSession 的回调,除了 seekTo 使用回调作为预期的。