MediaControllerCompat 的 PlaybackState

PlaybackState for MediaControllerCompat

我一直在 this guide 使用 MediaSessionCompat 构建后台音频应用程序,但遇到了问题。

在我的 Activity 中,我将 UI 连接到媒体控制器,如下所示

private MediaBrowserCompat.ConnectionCallback mediaBrowserConnectionCallbacks = new MediaBrowserCompat.ConnectionCallback() {
    @Override
    public void onConnected() {
        super.onConnected();
        try {
            mediaControllerCompat = new MediaControllerCompat(StreamPlayerActivity.this, mediaBrowserCompat.getSessionToken());
            mediaControllerCompat.registerCallback(mediaControllerCallbacks);
            mediaControllerCompat.setMediaController(StreamPlayerActivity.this, mediaControllerCompat);
            mediaControllerCompat.getTransportControls().playFromMediaId(String.valueOf(R.raw.hann), null);
            Log.d(TAG, "connected\t" + currentState + "\t" + mediaControllerCompat.getPlaybackState());
        } catch( RemoteException e ) {
            e.printStackTrace();
            Log.d(TAG, "connection failed");
        }
    }
};

private MediaControllerCompat.Callback mediaControllerCallbacks = new MediaControllerCompat.Callback() {
    @Override
    public void onPlaybackStateChanged(PlaybackStateCompat state) {
        super.onPlaybackStateChanged(state);
        if( state == null ) {
            return;
        }

        switch( state.getState() ) {
            case PlaybackStateCompat.STATE_PLAYING: {
                currentState = STATE_PLAYING;
                break;
            }
            case PlaybackStateCompat.STATE_PAUSED: {
                currentState = STATE_PAUSED;
                break;
            }
        }
    }
};

在我的 Service 中,我按如下方式构建我的会话

private MediaSessionCompat.Callback mediaSessionCallbacks = new MediaSessionCompat.Callback() {
    @Override
    public void onPlay() {
        super.onPlay();
        if( !successfullyRetrievedAudioFocus() ) {
            return;
        }

        mediaSessionCompat.setActive(true);
        setMediaPlaybackState(PlaybackStateCompat.STATE_PLAYING);

        mediaPlayer.start();
        Log.d(TAG, "mp started");
    }

    @Override
    public void onPause() {
        super.onPause();

        if( mediaPlayer.isPlaying() ) {
            mediaPlayer.pause();
            setMediaPlaybackState(PlaybackStateCompat.STATE_PAUSED);
        }
    }

    @Override
    public void onPlayFromMediaId(String mediaId, Bundle extras) {
        super.onPlayFromMediaId(mediaId, extras);

        try {
            AssetFileDescriptor afd = getResources().openRawResourceFd(Integer.valueOf(mediaId));
            if (afd == null) {
                return;
            }

            try {
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                Log.d(TAG, "datasource set");
            } catch (IllegalStateException e) {
                mediaPlayer.release();
                initMediaPlayer();
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            }

            afd.close();
            initMediaSessionMetadata();

        } catch (IOException e) {
            return;
        }

        try {
            mediaPlayer.prepare();
            Log.d(TAG, "media prepared");
        } catch (IOException e) { Log.d(TAG, "media preparation failed");
        }
    }
};

基本上,我已经逐行遵循网站的指南,除了我遗漏了显示当前音频信息的 Notification。在我的设备上进行测试时,出现 NullPointerException 错误。

logcat 发现这是在尝试 mediaControllerCompat.getPlaybackState().getState() 时发生的。我计算出我的 MediaControllerCompat.getPlaybackState()null。即使 MediaControllerCompatAndroid Devleopers Site 似乎也无法解决我的问题。也许这只是我,但在尝试检索其值之前,我没有看到任何定义 MediaControllerCompatPlaybackState 的代码。

我该如何解决这个问题?

提前感谢您的帮助。

这是一个愚蠢的错误。我必须在 mediaPlayer 准备好后添加 setMediaPlaybackState(PlaybackStateCompat.STATE_PLAYING);