4

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

我的问题是,我在第一个 Activity 中没有连接到 Chromecast 设备,然后转到第二个 Activity。在第二个中,我连接到 Chromecast 设备。它跑进了onRouteSelected()第二个Activity,然后跑进了onDeviceAvailable()->openSession()第一个Activity。
我不知道为什么它没有onDeviceAvailable()在第二个活动中遇到。
谁能帮我?

提前致谢

这是我在这两个活动中的代码:

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

2 回答 2

10

我编写了一个单独的 ChromecastAdapter Singleton 类来实现 MediaRouteAdapter。我在创建时调用 registerMinimalMediaRouteProvider 并且从不取消注册它。ChromecastAdapter 包含所有 Chromecast 状态。我在每个活动中所要做的就是传入我的 MediaRouteButton 并在其上调用 setRouteSelector。

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);
    }
    ...
}
于 2013-11-02T05:18:04.880 回答
1

我在这里发现了问题:
MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext, this );将在第二个 Activity 中返回 false。因为我已经在第一个Activity中注册过了。

我只是认为一个临时解决方案:

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

但是当我取消注册时,我必须再次连接到 Chromecast 设备。因为它会释放 CastContext 的所有状态。

有人帮忙吗?

于 2013-11-01T08:29:43.947 回答