Pergunta

I have been able to successfully cast video to a Chromecast and have the option let the video play when disconnecting and it all works great. However, if I choose to quit the application and let the video continue playing and then try to re-join the currently playing session and try to use the RemoteMediaPlayer to control the video I am getting: "java.lang.IllegalStateException: No current media session".

Just as a background, I am saving the route id and session id on the initial connect into preferences and am able to successfully call "Cast.CastApi.joinApplication" and when in the onResult I am recreating the Media Channel and setting the setMessageReceivedCallbacks like so:

Cast.CastApi.joinApplication(mApiClient,"xxxxxxxx",persistedSessionId).setResultCallback(new ResultCallback<Cast.ApplicationConnectionResult>() {
@Override
public void onResult(Cast.ApplicationConnectionResult applicationConnectionResult) {
    Status status = applicationConnectionResult.getStatus();
    if (status.isSuccess()) {
        mRemoteMediaPlayer = new RemoteMediaPlayer();
        mRemoteMediaPlayer.setOnStatusUpdatedListener(
        new RemoteMediaPlayer.OnStatusUpdatedListener() {
            @Override
            public void onStatusUpdated() {
                Log.d("----Chromecast----", "in onStatusUpdated");
            }
        });

        mRemoteMediaPlayer.setOnMetadataUpdatedListener(
        new RemoteMediaPlayer.OnMetadataUpdatedListener() {
            @Override
            public void onMetadataUpdated() {
                Log.d("----Chromecast----", "in onMetadataUpdated");
            }
        });

        try {
            Cast.CastApi.setMessageReceivedCallbacks(mApiClient,mRemoteMediaPlayer.getNamespace(),      mRemoteMediaPlayer);
        } catch (IOException e) {
           Log.e("----Chromecast----", "Exception while creating media channel", e);
        }


        //-----------RESOLUTION START EDIT------------------
        mRemoteMediaPlayer.requestStatus(mApiClient).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
            @Override
            public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {
                Status stat = mediaChannelResult.getStatus();
                if(stat.isSuccess()){
                    Log.d("----Chromecast----", "mMediaPlayer getMediaStatus success");
                    // Enable controls
                }else{
                    Log.d("----Chromecast----", "mMediaPlayer getMediaStatus failure");
                    // Disable controls and handle failure
                }
            }
        });
        //-----------RESOLUTION END EDIT------------------


    }else{
        Log.d("----Chromecast----", "in status failed");
    }
}
}

If I declare the RemoteMediaPlayer as static:

private static RemoteMediaPlayer mRemoteMediaPlayer;

I can join the existing session as well as control the media using commands like:

mRemoteMediaPlayer.play(mApiClient);

or

mRemoteMediaPlayer.pause(mApiClient);

But once I quit the application obviously the static object is destroyed and the app produces the aforementioned "No current media session" exception. I am definitely missing something because after I join the session and register the callback perhaps I need to start the session just like it was creating when I initially loaded the media using mRemoteMediaPlayer.load(.

Can someone please help as this is very frustrating?

Foi útil?

Solução

The media session ID is part of the internal state of the RemoteMediaPlayer object. Whenever the receiver state changes, it sends updated state information to the sender, which then causes the internal state of the RemoteMediaPlayer object to get updated.

If you disconnect from the application, then this state inside the RemoteMediaPlayer will be cleared.

When you re-establish the connection to the (still running) receiver application, you need to call RemoteMediaPlayer.requestStatus() and wait for the OnStatusUpdatedListener.onStatusUpdated() callback. This will fetch the current media status (including the current session ID) from the receiver and update the internal state of the RemoteMediaPlayer object accordingly. Once this is done, if RemoteMediaPlayer.getMediaStatus() returns non-null, then it means that there is an active media session that you can control.

Outras dicas

As user3408864 pointed out, requestStatus() after rejoining the session works. Here is how i managed to solve it in my case and it should work in yours.

     if(MAIN_ACTIVITY.isConnected()){

            if(MAIN_ACTIVITY.mRemoteMediaPlayer == null){
                MAIN_ACTIVITY.setRemoteMediaPlayer();
            }

            MAIN_ACTIVITY.mRemoteMediaPlayer.requestStatus(MAIN_ACTIVITY.mApiClient).setResultCallback( new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
                @Override 
                public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {
                    if(playToggle ==0){

                        try {
                            MAIN_ACTIVITY.mRemoteMediaPlayer.pause(MAIN_ACTIVITY.mApiClient);
                            playToggle =1;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else{

                        try {
                            MAIN_ACTIVITY.mRemoteMediaPlayer.play(MAIN_ACTIVITY.mApiClient);
                            playToggle =0;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            });

        }

Ignore, MAIN_ACTIVITY, it is just a static reference to my activity since i run this piece of code from a Service. Also, setRemoteMediaPlayer() is a method where i create a new RemoteMediaPlayer() and attach the corresponding Listeners.

Hopefully this helps. Also, sorry if any mistake, it is my first post to StackOverFlow.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top