0

我目前正在使用来自 Android 支持库的 MediaBrowser compat 使用客户端/服务方法开发一个简单的媒体播放器。

我收到以下异常:

FATAL EXCEPTION: main
Process: valdivia.peter.inclubooks, PID: 32383
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v4.media.session.PlaybackStateCompat.getState()' on a null object reference
at valdivia.peter.inclubooks.activities.MostrarAudiolibroActivity$3.onClick(MostrarAudiolibroActivity.java:118)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:at android.os.Handler.handleCallback(Handler.java:
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我的理解是控制器或播放状态为空,所以显然它不能返回表示播放器状态的 int。

以下是我如何在 MediaBrowser 回调的 onConnected() 方法中的客户端活动上创建控制器:

@Override
        public void onConnected() {
            super.onConnected();
            Log.d(tag,"Conecté al servicio");
            Log.d(tag,"token:" + mMediaBrowser.getSessionToken().toString());
            try{
                controller = new MediaControllerCompat(getApplicationContext(),mMediaBrowser.getSessionToken());
            }catch(RemoteException exception){
                Log.d(tag,"Remote Exception al crear el Media Controller");
                Toast.makeText(getApplicationContext(),"Error al crear los controles de la sesión",Toast.LENGTH_LONG).show();
                return;
            }
            controller.registerCallback(mControllerCallback);
            MediaControllerCompat.setMediaController(MostrarAudiolibroActivity.this,controller);
            buildTransportControls();
        }

在这里我如何构建传输控件:

private void buildTransportControls() {
    ImageButton mBotonPausa = (ImageButton) findViewById(R.id.mostrar_audiolibro_playpause);
    mBotonPausa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MediaControllerCompat mediaControllerCompat = MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this);
            int estado = mediaControllerCompat.getPlaybackState().getState();
            if(estado == PlaybackStateCompat.STATE_PLAYING){
                MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().pause();
            }else if (estado == PlaybackStateCompat.STATE_PAUSED){
                MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().play();
            }else{
                Bundle extras = new Bundle();
                extras.putString("titulo",MostrarAudiolibroActivity.this.audiolibro.getTitulo());
                extras.putString("autor",MostrarAudiolibroActivity.this.audiolibro.getAutor());
                MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().playFromUri(Uri.parse(MostrarAudiolibroActivity.this.audiolibro.getLinkAudiolibro()),extras);
                Log.d(tag,"Voy a tratar de abrir el audio");
            }
        }
    });
    ImageButton mBotonReiniciar = (ImageButton) findViewById(R.id.mostrar_audiolibro_reiniciar);
    mBotonReiniciar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().seekTo(0);
            //MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().play();
        }
    });


}

我做错了什么。提前致谢。

4

1 回答 1

2

对 can的调用MediaControllerCompat.getPlaybackState()返回 null。该方法的文档说返回 null 是可能的。您必须更改代码以允许这种可能性。

更具体地说,该行:

int estado = mediaControllerCompat.getPlaybackState().getState();

必须分成多个部分:

PlaybackStateCompat playbackStateCompat = mediaControllerCompat.getPlaybackState();
if (playbackStateCompat == null) {
    // handle the case that it's null
} else {
    int estado = playbackStateCompat.getState();
}
于 2017-09-24T04:57:17.927 回答