cc5zhenhua
cc5zhenhua

Reputation: 145

Why my MediaRouteButton not available to find any cast devices?

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.

Upvotes: 1

Views: 2614

Answers (2)

cc5zhenhua
cc5zhenhua

Reputation: 145

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.

Upvotes: 0

Ali Naddaf
Ali Naddaf

Reputation: 19034

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

Upvotes: 2

Related Questions