Chromecast Android Sender RemoteMediaPlayer producing No current media session

和自甴很熟 提交于 2019-12-01 14:27:34

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!