Android MediaSessionCompat未在KitKat上显示媒体控制按钮

时间:2015-07-20 19:39:04

标签: android android-mediasession

我正在制作音乐播放器,目前正在尝试实施锁屏媒体控制和播放信息。我的第一次尝试是使用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。

0 个答案:

没有答案