1

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.

cc5zhenhua
  • 145
  • 3
  • 15

2 Answers2

2

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

Community
  • 1
  • 1
Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
  • Thank you. I have a question that: the mMediaRouter and mMediaRouteButton seems not related, even if I add callback to mMediaRouter, how makes the mMediaRouteButton available? Have I missed any configuration missed for related mMediaRouter to mMediaRouteButton? – cc5zhenhua Feb 24 '14 at 01:57
  • 1
    MediaRouter and MediaRouterButton are related internally, in the sense that the behavior of the button is managed by the MediaRouter framework. The easiest approach, in general, is to use the MediaRouteActionProvider. If you can use that, it is recommended to do so; there are a number of samples there on our github repo that use that. Adding the callback as I mentioned earlier starts the (mDNS) scan for cast devices and using your selector criteria, it tries to find the available candidates that would show up if you click on the cast button, regardless of which one of two approaches you take. – Ali Naddaf Feb 24 '14 at 02:22
  • I create : private class MyMediaRouterCallback extends MediaRouter.Callback .....and with the call back added but the button still not available. Any other possibilities? mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); – cc5zhenhua Feb 24 '14 at 13:40
  • Hi Ali Naddaf, I got following text about the MediaRouteButton in android documents."When the default route is selected or when the currently selected route does not match the selector, the button will appear in an inactive state ." Is there any possible that a default route has been selected automatically? – cc5zhenhua Feb 24 '14 at 15:19
  • If there are no routes available, then the button shouldn't show up. If there are routes available but you have not selected any of them, it means the default route is selected and the button appears in inactive state. As I suggested before, use the MediaRouteActionProvider if you can. If not, I suggest you start with one of the github samples and work your way up from there; without having access to your code, it is very difficult to see what is happening. – Ali Naddaf Feb 24 '14 at 15:32
  • I didn't select the ActionBar option because it request the activity to extends ActionBarActivity.(I have my main activity extends another class and it will be a lot work to change that). But I think the mediaRouteButton also support the feature. It has the mMediaRouteButton.setRouteSelector(mMediaRouteSelector); I set the button always show up . If possible, please share me your email address and I'll sent you all my project. – cc5zhenhua Feb 24 '14 at 16:00
  • I suggest you write a very simple activity that exhibits your issue with MediaRouteButton and post that here; it is best to get a simple case going and then you can add the complexities of your project to that; so start with a fresh activity and MediaRouteButton and callbacks, etc and see if that works or not. If it doesn't, then post the activity code here, which should be a very small file. – Ali Naddaf Feb 24 '14 at 16:37
  • Hi Ali, I created a simple test MediaRouteCast project as following: 1. new android project with v7-appcompat,v7-routemedia android reference. 2. Update AndroidManifest.xml to grant permissions. 3. In Menu--main.xml, add item as following: 4. in MainActivity input code as below: – cc5zhenhua Feb 25 '14 at 12:23
  • public void onCreate(Bundle savedinstancestate) { } – cc5zhenhua Feb 25 '14 at 12:24
  • public void onCreate(Bundle savedinstancestate) {super.onCreate(savedinstancestate); setContentView(R.layout.activity_main); mMediaRouter=MediaRouter.getInstance(getApplicationContext()); mMediaRouteSelector = new MediaRouteSelector.Builder().addControlCategory(CastmediaControlIntent.CATEGORY_CAST).build(); mMediaCallback = new MyMediaRouterCallback(); mMediaRouter.addCallback(mMediaRouteSelector,mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); – cc5zhenhua Feb 25 '14 at 12:30
  • public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); MenuItem mediaRouteItem = menu.findItem( R.id.action_mediaroute ); mMediaRouteButton = (MediaRouteButton) mediaRouteItem.getActionView(); mMediaRouteButton.setRouteSelector(mMediaRouteSelector); return true; } – cc5zhenhua Feb 25 '14 at 12:30
  • private class MyMediaRouterCall extends MediaRouter.Callback – cc5zhenhua Feb 25 '14 at 12:32
  • { public void onRouteSelected(MediaRouter router,RouteInfo info) {mSelectedDevice=CastDevice.getfromBundle(info.getExtras());} public void onRouteUnselected(MediRouter router,RouteInfo info){ mSelectedDevice=null;} } – cc5zhenhua Feb 25 '14 at 12:34
  • No build error and run error with above project, except the cast button always in inactive state and not clickable. – cc5zhenhua Feb 25 '14 at 12:39
  • My mainactivity extends android.support.v4.app.FragmentActivity. – cc5zhenhua Feb 25 '14 at 12:54
  • Please put your code in the original post (i.e. update your post with the new code), it is impossible to follow that in a number of comments that you have used. – Ali Naddaf Feb 25 '14 at 15:27
  • Also, I suggest you grab the existing Android sample apps and try them to see if they work for you or not; may be the simplest would be CastHelloText-android. – Ali Naddaf Feb 25 '14 at 15:29
  • Hi Ali, I run the CastHelloText-android and can not get the cast button available either. But the CastSampleActivity with old GoogleCastSdkAndroid.jar works well. – cc5zhenhua Feb 25 '14 at 16:09
  • I also validate my code in the main activity and didn't found any suspicious points. I think it may be my project config issue. When I create the android app, I set Minimu required SDK,Target SDK,Compile with all to API 16. Is this the problem? – cc5zhenhua Feb 25 '14 at 16:12
0

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.

cc5zhenhua
  • 145
  • 3
  • 15