我正在使用此example code来构建MediaPlayer Service。我或多或少都在工作,但是如果用户返回到包含媒体控件的Activity,则需要检测MediaPlayer处于什么状态。我当前使用的代码会引发NullReferenceException错误在onResume中,因为getPlaybackState()始终为null

我是使用MediaSessionCompat的新手,根据文档,我可以某种方式获取当前会话:

“一旦创建了会话,会话的所有者就可以将其会话令牌传递给其他进程,以允许它们创建MediaControllerCompat来与会话进行交互。”

    public class MediaActivity extends AppCompatActivity {

    private MediaBrowserCompat mMediaBrowserCompat;
    private MediaControllerCompat mMediaControllerCompat;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_media);

        mActivity = this;
        mPlayButton = (Button)findViewById(R.id.btn_play);

        mMediaBrowserCompat = new MediaBrowserCompat(
                getApplicationContext(),
                new ComponentName(mContext, MediaPlayerService.class),
                mMediaBrowserCompatConnectionCallback,
                getIntent().getExtras()
        );

        mMediaBrowserCompat.connect();

        mPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( mCurrentState == STATE_PAUSED ) {
                        getSupportMediaController().getTransportControls().play();
                        mCurrentState = STATE_PLAYING;
                        mPlayButton.setText("Pause");
                } else {
                    MediaControllerCompat.getMediaController(mActivity).getTransportControls().pause();
                    mCurrentState = STATE_PAUSED;
                    mPlayButton.setText("Play");
                }
            }
        });

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

            if (MediaControllerCompat.getMediaController(mActivity).getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING) {
                mPlayButton.setText("Pause")
            }
            else{
                mPlayButton.setText("Play")
            }

        }

        private MediaBrowserCompat.ConnectionCallback mMediaBrowserCompatConnectionCallback = new MediaBrowserCompat.ConnectionCallback() {

            @Override
            public void onConnected() {
                super.onConnected();
                try {
                    mMediaControllerCompat = new MediaControllerCompat(PodcastEpisodeActivity.this, mMediaBrowserCompat.getSessionToken());
                    mMediaControllerCompat.registerCallback(mMediaControllerCompatCallback);
                    MediaControllerCompat.setMediaController(mActivity, mMediaControllerCompat);
                    MediaControllerCompat.getMediaController(mActivity).getTransportControls().playFromUri(Uri.parse("http://www.url.com"), extras);

                } catch( RemoteException e ) {
                    Log.e(mActivity.getPackageName(), e.toString());
                }
            }
        };


        private MediaControllerCompat.Callback mMediaControllerCompatCallback = new MediaControllerCompat.Callback() {

            @Override
            public void onPlaybackStateChanged(PlaybackStateCompat state) {
                super.onPlaybackStateChanged(state);

                if (state == null ) return;

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

最佳答案

我无法通过可用的API找到一种方法,所以我只是在跟踪通过本地存储(sqlite)播放的媒体,并通过该媒体更新显示。可能不是最优雅的解决方案,但它可行。

10-08 05:01