0

我按照本指南使用 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。甚至Android Devleopers SiteMediaControllerCompat似乎也无法解决我的问题。也许只是我,但在尝试检索其值之前,我没有看到任何定义MediaControllerCompat's 的代码。PlaybackState

我该如何解决这个问题?

提前感谢您的帮助。

4

1 回答 1

0

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

于 2018-12-25T06:47:46.037 回答