Search code examples
androidcastingchromecastgoogle-cast

Cast button not displaying


I know its a quite common problem although searching through stackoverflow and android developers site left me without whole picture.

  1. Is it necessary to register application in Google Cast SDK Developer Console? I don't need custom receiver so I can use CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID, can I?
  2. If I register application and receive its ID but do NOT publish it, does it let me to use Google Cast features?
  3. Registering Devices. Do I need to register mobile phone's serial number or Chromecast's? Or both?

I belive the problem lies there but still here's the code. Probably Cast Button doesn't show for some other reason.

Manifest:

...
    android:theme="@style/Theme.AppCompat">

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
...

Media Router menu, media_router.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/media_route_menu_item"
        android:title="@string/media_router_menu_title"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        app:showAsAction="always"/>
</menu>

Graddle dependencies:

compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:mediarouter-v7:21.0.0'
compile 'com.google.android.gms:play-services:7.3.0'

buildMediaRouter() called in onCreate():

private void buildMediaRouter(){
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());

    // Create a route selector for the type of routes your app supports.
    mMediaRouteSelector = new MediaRouteSelector.Builder()
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
            .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
            //default app ID because I don't need custom Receiver
            .addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
            .build();
}

Creating options menu:

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    super.onCreateOptionsMenu(menu);
    // Inflate the menu and configure the media router action provider.
    getMenuInflater().inflate(R.menu.media_router, menu);

    // Attach the MediaRouteSelector to the menu item
    MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
    MediaRouteActionProvider mediaRouteActionProvider =
            (MediaRouteActionProvider) MenuItemCompat.getActionProvider(
                    mediaRouteMenuItem);
    mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);

    return true;
}

Appreciating any help.


Solution

  • To answer your questions:

    1. As long as you do not need a custom or styled receiver and are fine with using the Default Receiver, then you do not need to register anything.
    2. If you register an app but do not publish that, then it will be visible to only those cast devices that you have added to your registration, no other cast device will be able to load your app.
    3. If you register an app, only the registered cast devices will be able to see your app (as long as your app is not published). Once you publish your app, any cast device will be able to load your app. Registration of a device, for development purposes, is only for the cast devices and not the sender devices.

    After registering your cast device, wait a few minutes and reboot your device so it can pick up the new configuration. One common issue is registering the right serial number for your cast device, it is easy to mistake 0 and O, etc so take a photo of the serial number and zoom in to read the value.