I am trying to create a ChomeCast sender app with Android Studio. I have implemented all code need to be able to Add the Cast Button but it's not available
Here is my Code.
build.gradle
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.google.android.gms:play-services:4.3.+'
compile 'com.android.support:mediarouter-v7:19.+'
}
res/menu/main.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.castbuttontest.app.MainActivity" >
<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>
MainActivity.java
package com.example.castbuttontest.app;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.cast.Cast;
import com.google.android.gms.cast.CastDevice;
import com.google.android.gms.cast.CastMediaControlIntent;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends ActionBarActivity {
MediaRouter mediaRouter;
MediaRouteSelector mediaRouteSelector;
MediaRouter.Callback myMediaRouterCallBack = new MediaRouter.Callback() {
@Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
CastDevice device = CastDevice.getFromBundle(route.getExtras());
}
@Override
public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
String APP_ID = "F6D3E50B";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaRouter = MediaRouter.getInstance(this);
mediaRouteSelector = new MediaRouteSelector.Builder()
//.addControlCategory(CastMediaControlIntent.categoryForCast(APP_ID))
.addControlCategory(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)
.build();
}
@Override
protected void onStart() {
super.onStart();
mediaRouter.addCallback(mediaRouteSelector, myMediaRouterCallBack,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}
@Override
protected void onStop() {
mediaRouter.removeCallback(myMediaRouterCallBack);
super.onStop();
}
@Override
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 mediaRouterItem = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider provider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouterItem);
provider.setRouteSelector(mediaRouteSelector);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
Can you tell where the problem is located? Thanks in Advance
EDIT: I have finally solved the problem .I have created a new Empty project and re-written the code by following the Sender App tutorial (Link):
MainActivity.java
/*
Copyright 2014 Charles-Eugene LOUBAO
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cf.charly1811.android.sample.MediaRouteProvider;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.cast.CastDevice;
import com.google.android.gms.cast.CastMediaControlIntent;
public class MainActivity extends ActionBarActivity {
/**
* This sample shows how to add the Cast Button to the action bar using MediaRouterProvider
* @see "https://developers.google.com/cast/docs/android_sender"
*/
public static final String TAG = MainActivity.class.getSimpleName();
String APP_ID = "F6D3E50B";
MediaRouter mediaRouter;
MediaRouteSelector mediaRouteSelector;
MediaRouter.Callback mMediaRouterCallback;
CastDevice mCastDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaRouter = MediaRouter.getInstance(this);
mediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(APP_ID))
.build();
mMediaRouterCallback = new MediaRouter.Callback() {
@Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteSelected(router, route);
Log.d(TAG, "Connected to "+ route.getName());
mCastDevice = CastDevice.getFromBundle(route.getExtras());
Toast.makeText(getApplicationContext(), "Connected to "+route.getName(),Toast.LENGTH_LONG).show();
}
@Override
public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteUnselected(router, route);
mCastDevice = null;
}
};
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
mediaRouter.addCallback(mediaRouteSelector,mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
if(isFinishing())
{
mediaRouter.removeCallback(mMediaRouterCallback);
}
}
@Override
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 media_route_menu_item = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider provider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(media_route_menu_item);
provider.setRouteSelector(mediaRouteSelector);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
/*
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
*/
return super.onOptionsItemSelected(item);
}
}
And now it works properly. The Icon is now available and I can connect to my Chromecast. I am not sure but I think the problem was about the Instance of MediaRouter.Callback;
PS: I have uploaded the full code on GitHub . You can check it out. (Link)
Scroll down to the Devices section and look for your Chromecast. If your Chromecast is listed, it is on the same network as your Android. If your Chromecast isn't listed, it may be connected to a different network than your Android. Visit the Google support website for help switching networks.
Give the devices a moment to reconnect and then simply try again. Another solution may be to restart the apps you are using on your smartphone or tablet, for example. Close them entirely and then restart them. If that doesn't solve the problem, you might need to restart your Chromecast.
Rewriting it all from beginning doesn't actually show anyone where the problem was...
You have
.addControlCategory(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)
instead of
.addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
You were passing APP_ID directly into category. I guess category has special format and APP_ID isn't exactly that. I did the same mistake. :)
I have finally solved the problem .I have created a new Empty project and re-written the code by following the Sender App tutorial (Link):
MainActivity.java
/*
Copyright 2014 Charles-Eugene LOUBAO
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cf.charly1811.android.sample.MediaRouteProvider;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.cast.CastDevice;
import com.google.android.gms.cast.CastMediaControlIntent;
public class MainActivity extends ActionBarActivity {
/**
* This sample shows how to add the Cast Button to the action bar using MediaRouterProvider
* @see "https://developers.google.com/cast/docs/android_sender"
*/
public static final String TAG = MainActivity.class.getSimpleName();
String APP_ID = "F6D3E50B";
MediaRouter mediaRouter;
MediaRouteSelector mediaRouteSelector;
MediaRouter.Callback mMediaRouterCallback;
CastDevice mCastDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaRouter = MediaRouter.getInstance(this);
mediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(APP_ID))
.build();
mMediaRouterCallback = new MediaRouter.Callback() {
@Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteSelected(router, route);
Log.d(TAG, "Connected to "+ route.getName());
mCastDevice = CastDevice.getFromBundle(route.getExtras());
Toast.makeText(getApplicationContext(), "Connected to "+route.getName(),Toast.LENGTH_LONG).show();
}
@Override
public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteUnselected(router, route);
mCastDevice = null;
}
};
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
mediaRouter.addCallback(mediaRouteSelector,mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
if(isFinishing())
{
mediaRouter.removeCallback(mMediaRouterCallback);
}
}
@Override
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 media_route_menu_item = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider provider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(media_route_menu_item);
provider.setRouteSelector(mediaRouteSelector);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
/*
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
*/
return super.onOptionsItemSelected(item);
}
}
And now it works properly. The Icon is now available and I can connect to my Chromecast. I am not sure but I think the problem was about the Instance of MediaRouter.Callback;
PS: I have uploaded the full code on GitHub . You can check it out. (Link)
I have struggled to find a solution to this, and nothing I found helped. Until I eventually found this link
A Unavailable: While Cast receivers are not available, the Cast button does not appear
Check if you have any devices to CAST TO in your vicinity. I didn't, so the button was hidden by design. You can easily just check, go to youtube app, and there's no cast button there either when there's not a receiver app present. Hope this saves someone some time cause I had a hard time trying to see why it isn't appearing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With