本文介绍了Chromecast的多个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发同时支持的Chromecast和ActionbarSherlock的应用程序。
在每一个活动中,我创建了一个MediaRouteButton在动作条。

我的问题是,我没有连接到的Chromecast设备中的第一个活动,然后转到第二个活动。在第二个,我连接到​​的Chromecast设备。它拼命地跑入 onRouteSelected()第二个活动,那么拼命地跑入 onDeviceAvailable() - > 的openSession()第一项活动。
我不知道为什么它没有在第二活动碰上 onDeviceAvailable()
谁能帮我?

在此先感谢

下面是我的code在这两个活动:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_cast_sample);

    mMetaData =新ContentMetadata();


    mMediaSelectionDialog =新MediaSelectionDialog(本);
    mCastContext =新CastContext(getApplicationContext());
    MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext,这一点);
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST);
    mMediaRouterCallback =新MyMediaRouterCallback();
}
   @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        // TODO自动生成方法存根
        。getSupportMenuInflater()膨胀(R.menu.cast_sample,菜单);
        com.actionbarsherlock.view.MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
        mMediaRouteButton =(MediaRouteButton)mediaRouteMenuItem.getActionView();
        mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
        返回true;
    }


    @覆盖
    保护无效的OnStart(){
        super.onStart();
        mMediaRouter.addCallback(mMediaRouteSelector,mMediaRouterCallback,
                MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    }



    @覆盖
    保护无效的onStop(){
        mMediaRouter.removeCallback(mMediaRouterCallback);
        super.onStop();
    }

  私有类MyMediaRouterCallback扩展MediaRouter.Callback {
        @覆盖
        公共无效onRouteSelected(MediaRouter路由器,RouteInfo路线){
            MediaRouteHelper.requestCastDeviceForRoute(路线);
        }

        @覆盖
        公共无效onRouteUnselected(MediaRouter路由器,RouteInfo路线){
            尝试 {
                如果(mSession!= NULL){
                    logVIfEnabled(TAG,结束会话,并停止应用程序);
                    mSession.setStopApplicationWhenEnding(真正的);
                    mSession.endSession();
                } 其他 {
                    Log.e(TAG,onRouteUnselected:mSession为空);
                }
            }赶上(IllegalStateException异常E){
                Log.e(TAG,onRouteUnselected:);
                e.printStackTrace();
            }赶上(IOException异常E){
                Log.e(TAG,onRouteUnselected:);
                e.printStackTrace();
            }
            mMessageStream = NULL;
            mSelectedDevice = NULL;
        }
    }


  @覆盖
  公共无效onDeviceAvailable(CastDevice设备,字符串myString的,
        MediaRouteStateChangeListener监听器){
        mSelectedDevice =设备;
        logVIfEnabled(TAG,可用的设备中找到:+ myString的);
        的openSession();
  }
  私人无效的openSession(){
        mSession =新ApplicationSession(mCastContext,mSelectedDevice);
        .....
  }
 

解决方案

我写了一个实现MediaRouteAdapter单独ChromecastAdapter Singleton类。我呼吁registerMinimalMediaRouteProvider的创作,从不注销它。该ChromecastAdapter包含所有的Chromecast状态。所有我需要做的每一项活动是通过我的MediaRouteButton,并呼吁setRouteSelector就可以了。

 公共类ChromeCastAdapter实现MediaRouteAdapter {

    ...
    私有静态ChromeCastAdapter实例= NULL;

    公共静态ChromeCastAdapter的getInstance(上下文活动){
        如果(例如== NULL){
            例如=新ChromeCastAdapter(活动);
        }
        返回实例;
    }

    私人ChromeCastAdapter(上下文活动){
        this.context = activity.getApplicationContext();

        castContext =新CastContext(上下文);
        mediaRouter = MediaRouter.getInstance(上下文);

        MediaRouteHelper.registerMinimalMediaRouteProvider(castContext,这一点);

        mediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST);

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

    公共无效setMediaRouteButtonSelector(MediaRouteButton mediaRouteButton){
        mediaRouteButton.setRouteSelector(mediaRouteSelector);
    }
    ...
}
 

I'm developing an application supporting both Chromecast and ActionbarSherlock.
In every activity, I create a MediaRouteButton in the ActionBar.

My problem is, I didn't connect to the Chromecast device in the first Activity, and go to the second Activity. In the second one, I connect to the Chromecast device. It runned into the onRouteSelected() of the second Activity, then runned into onDeviceAvailable() -> openSession() of the first Activity.
I don't know why it didn't run into onDeviceAvailable() on the second Activity.
Can anyone help me?

Thanks in advance

Here is my code in both activities:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cast_sample);

    mMetaData = new ContentMetadata();


    mMediaSelectionDialog = new MediaSelectionDialog(this);
    mCastContext = new CastContext( getApplicationContext());
    MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext, this );
    mMediaRouter = MediaRouter.getInstance( getApplicationContext() );
    mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST );
    mMediaRouterCallback = new MyMediaRouterCallback();
}
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        getSupportMenuInflater().inflate(R.menu.cast_sample, menu);
        com.actionbarsherlock.view.MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
        mMediaRouteButton = (MediaRouteButton) mediaRouteMenuItem.getActionView();
        mMediaRouteButton.setRouteSelector( mMediaRouteSelector );
        return true;
    }


    @Override
    protected void onStart() {
        super.onStart();
        mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    }



    @Override
    protected void onStop() {
        mMediaRouter.removeCallback(mMediaRouterCallback);
        super.onStop();
    }

  private class MyMediaRouterCallback extends MediaRouter.Callback {
        @Override
        public void onRouteSelected(MediaRouter router, RouteInfo route) {
            MediaRouteHelper.requestCastDeviceForRoute(route);
        }

        @Override
        public void onRouteUnselected(MediaRouter router, RouteInfo route) {
            try {
                if (mSession != null) {
                    logVIfEnabled(TAG, "Ending session and stopping application");
                    mSession.setStopApplicationWhenEnding(true);
                    mSession.endSession();
                } else {
                    Log.e(TAG, "onRouteUnselected: mSession is null");
                }
            } catch (IllegalStateException e) {
                Log.e(TAG, "onRouteUnselected:");
                e.printStackTrace();
            } catch (IOException e) {
                Log.e(TAG, "onRouteUnselected:");
                e.printStackTrace();
            }
            mMessageStream = null;
            mSelectedDevice = null;
        }
    }


  @Override
  public void onDeviceAvailable(CastDevice device, String myString,
        MediaRouteStateChangeListener listener) {
        mSelectedDevice = device;
        logVIfEnabled(TAG, "Available device found: " + myString);
        openSession();
  }
  private void openSession() {
        mSession = new ApplicationSession(mCastContext, mSelectedDevice);
        .....
  }
解决方案

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);
    }
    ...
}

这篇关于Chromecast的多个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 07:30