Chromecast with multiple activity

一笑奈何 提交于 2019-12-03 16:47:59

I wrote a separate ChromecastAdapter Singleton class that implements MediaRouteAdapter. I call registerMinimalMediaRouteProvider on creation and never unregister it. The ChromecastAdapter contains all the Chromecast state. All I have to do in each activity is pass in my MediaRouteButton and call setRouteSelector on it.

public class ChromeCastAdapter implements MediaRouteAdapter { 

    ...
    private static ChromeCastAdapter instance = null;

    public static ChromeCastAdapter getInstance(Context activity) {
        if (instance == null) {
            instance = new ChromeCastAdapter(activity);
        }
        return instance;
    }

    private ChromeCastAdapter(Context activity) {
        this.context = activity.getApplicationContext();

        castContext = new CastContext(context);
        mediaRouter = MediaRouter.getInstance(context);

        MediaRouteHelper.registerMinimalMediaRouteProvider(castContext, this);

        mediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST);

        mediaRouterCallback = new MediaRouterCallback();
        mediaRouter.addCallback(mediaRouteSelector, mediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    }

    public void setMediaRouteButtonSelector(MediaRouteButton mediaRouteButton) {
        mediaRouteButton.setRouteSelector(mediaRouteSelector);
    }
    ...
}

I found the issue here:
MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext, this ); will return false in the second Activity. Because I already registered it in the first Activity.

I just think a temporary solution that:

  @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext, this );
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MediaRouteHelper.unregisterMediaRouteProvider(mCastContext);
    }

But When I unregister, I have to connect to the Chromecast device again. Because It'll release all the state of CastContext.

Does anyone helps anyidea?

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