为什么我的MediaRouteButton无法找到任何演员设备?

时间:2014-02-23 15:44:29

标签: google-cast chromecast

以下是我在主要活动中的代码

public class MainActivity extends ActionBarActivity{
private MediaRouteButton mMediaRouteButton;
private MediaRouteSelector mMediaRouteSelector;
private MediaRouter mMediaRouter;
private CastDevice mSelectedDevice;
private MyMediaRouterCallback mMediaRouterCallback;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(this.checkGooglePlaySevices(this))
        Log.v("cc5zhenhua","googleplayservice okay");
    else
    {
        Log.v("cc5zhenhua","googleplayservice not ok");
        //GooglePlayServicesUtil.getErrorDialog(0, this, 0).show();
    }
    //initialize media cast objects
     mMediaRouter=MediaRouter.getInstance(getApplicationContext());      
     mMediaRouteSelector=new MediaRouteSelector.Builder()
     .addControlCategory(CastMediaControlIntent.CATEGORY_CAST).build();      
     mMediaRouterCallback= new MyMediaRouterCallback();
     mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback);    
}

public void onStart() {
    super.onStart();
    mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback
            );
    MediaRouter.RouteInfo route = mMediaRouter.updateSelectedRoute(mMediaRouteSelector);
    // do something with the route...
}
@Override
protected void onResume()
{
    super.onResume();
    mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    super.onCreateOptionsMenu(menu);

    //mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
    return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem mediaRouteItem = menu.findItem( R.id.action_mediaroute01 );
    MediaRouteActionProvider mediaRouteActionProvider =
            (MediaRouteActionProvider)MenuItemCompat.getActionProvider(
                    mediaRouteItem);
    mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
    mMediaRouteButton = (MediaRouteButton) mediaRouteItem.getActionView();
    return true;}

 public  boolean checkGooglePlaySevices(final Activity activity) {
        final int googlePlayServicesCheck = GooglePlayServicesUtil.isGooglePlayServicesAvailable(
                activity);
        switch (googlePlayServicesCheck) {
            case ConnectionResult.SUCCESS:
                return true;
            default:
               Log.v("cc5zhenhua","test");          }
        return false;
 }
private class MyMediaRouterCallback extends MediaRouter.Callback 
{
  @Override
  public void onRouteSelected(MediaRouter router, RouteInfo info) {
    mSelectedDevice = CastDevice.getFromBundle(info.getExtras());
    String routeId = info.getId();
    Log.v("cc5zhenhua", "MainActivity.onRouteSelected");        
  }
  @Override
  public void onRouteUnselected(MediaRouter router, RouteInfo info) {
    //teardown();
    mSelectedDevice = null;
  }
}

}

没有构建错误。但是,当我运行主活动时,根本无法单击媒体路由按钮。请告诉我错过的地方?谢谢!

在发布新SDK之前,我的chromecast已列入白名单并使用APPID进行注册。 我不能将该appID用于控件类别,它会抛出不是有效的appID异常。

我的演员设备也可用于计算机中的chromecast扩展程序。

2 个答案:

答案 0 :(得分:1)

您需要通过添加回调来开始扫描:

mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);

如果您已经这样做并忘记在帖子中提及,那么您需要在开发者控制台上注册您的应用和设备。那么,您的问题很可能是由于您设备的白名单;尝试从http://<chromecast-ip>:9222的Chrome浏览器连接到您的设备,如果您不能,那么您的设备不会列入白名单;按照此post中的步骤排除故障

答案 1 :(得分:0)

最后得到问题点。仅仅因为我的旧googlecast sdk的最后一个应用程序适用于AVD,所以我专注于我的代码和新的SDK设置。但是,当我在真实手机上部署应用程序时,可以找到媒体路径。感谢阿里的善意和帮助。