I went through this https://developers.google.com/cast/docs/android_sender up to "Handling device selection" paragraph including. I expect that cast button should appear in action bar, but it isn't. And I suppose that Chromecast device was not been discovered.
I have real Chromecast device and I registered it in Google Cast SDK Developer Console. And I suppose that device whitelisted because I have access to http:// chromecast_device_ip:9222
I set "checked" for option "Send this Chromecast's serial number when checking for updates" 2 days ago. And I rebooted the device after that.
Also, I registered an application in Google Cast SDK Developer Console. But it's not published yet. I think it can't be the reason, isn't it?
I use DEFAULT_MEDIA_RECEIVER_APPLICATION_ID as APP_ID. But I also tried APP_ID of my registered application.
I tried sample apps from GitHub: https://github.com/googlecast Cast Tic-Tac-Toe, CastVideos-android, MediaRouter-Cast-Button-android. And in all sample apps was the same problem. Cast button doesn't appear.
In DIAL CLient app (author entertailion) all work fine. But I'm not sure that it is what I need to implement Chromecast sender app...
here is my code:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testcastbtn"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
package com.example.testcastbtn;
import com.google.android.gms.cast.CastDevice;
import com.google.android.gms.cast.CastMediaControlIntent;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.support.v7.media.MediaRouter.RouteInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private MediaRouter mMediaRouter;
private MediaRouteSelector mMediaRouteSelector;
private MyMediaRouterCallback mMediaRouterCallback;
public CastDevice mSelectedDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
mMediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(
CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
.build();
mMediaRouterCallback = new MyMediaRouterCallback();
int isGAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
}
private class MyMediaRouterCallback extends MediaRouter.Callback {
@Override
public void onRouteSelected(MediaRouter router, RouteInfo info) {
mSelectedDevice = CastDevice.getFromBundle(info.getExtras());
}
@Override
public void onRouteUnselected(MediaRouter router, RouteInfo info) {
mSelectedDevice = null;
}
}
@Override
protected void onStart() {
super.onStart();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}
@Override
protected void onResume() {
super.onResume();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}
@Override
protected void onPause() {
if (isFinishing()) {
mMediaRouter.removeCallback(mMediaRouterCallback);
}
super.onPause();
}
@Override
protected void onStop() {
mMediaRouter.removeCallback(mMediaRouterCallback);
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider mediaRouteActionProvider =
(MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.media_route_menu_item) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.testcastbtn.MainActivity" >
<item
android:id="@+id/test_menu_item"
android:title="@string/test_menu_title"
app:showAsAction="always"/>
<item
android:id="@+id/media_route_menu_item"
android:title="@string/media_route_menu_title"
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always"/>
</menu>
Also, I got next errors in LogCat. But it not crashes app.
08-01 16:21:17.644: E/ExternalAccountType(547): Unsupported attribute readOnly
08-01 16:21:19.196: E/MediaProvider(21126): invalid album art, error creating album thumb file
08-01 16:21:22.249: E/dalvikvm(21520): Could not find class 'com.google.android.apps.youtube.app.remote.bi', referenced from method com.google.android.apps.youtube.app.remote.bh.<init>
08-01 16:21:22.269: E/dalvikvm(21520): Could not find class 'com.google.android.apps.youtube.core.player.notification.a', referenced from method com.google.android.apps.youtube.core.player.notification.ExternalPlaybackControllerV14.<init>
UPDATE
Leon Nicholls said that it is known discovery issue in the previous release of the Cast SDK. A new version 5.0.89 of Google Play Services should work fine.
I suppose that I have an old release of the Cast SDK. In google-play-services_lib/AndroidManifest.xml I see next
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="5077000" android:versionName="5.0.77-000"
But I have no idea how to update it. In Android SDK Manager Google Play Services marked as installed and there is nothing to update....
I just had to wait some time (in my case I went to sleep and after 8 hours it worked).