Why my MediaRouteButton not available to find any cast devices?

谁说我不能喝 提交于 2019-12-13 17:57:01

问题


following are my codes in main activity

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

}

There's no build error. However when I run the main activity, the media route button can not be clicked at all. Please advise any where I missed? Thank you!

My chromecast is whitelisted registed with an APPID before the new SDK published. I can't use that appID for the control category either, it throws not valida appID exception.

My cast device is also available for chromecast extension in my computer.


回答1:


You need to start the scan by adding callbacks:

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

If you are already doing that and forgot to mention that in your post, then you need to register your app and device on the Developer Console. Your issue is, then, most likely due to the whitelisting of your device; try connecting to your device from a chrome browser at http://<chromecast-ip>:9222, if you can't, then your device is not whitelisted; follow the steps in this post to trouble shoot that




回答2:


Finally get the issue point. Just because that my last app with old googlecast sdk works on the AVD, so I focused on my codes and new SDK setting.However, when I deploy the app on real phone ,the media route can be found. Thanks to Ali for his kindness and helping.



来源:https://stackoverflow.com/questions/21970422/why-my-mediaroutebutton-not-available-to-find-any-cast-devices

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