7

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)

Charles-Eugene Loubao
  • 1,080
  • 2
  • 12
  • 22
  • Try duplicating your `app:` attributes with the `android:` prefix, so you will have both `app:showAsAction` and `android:showAsAction`, and both `app:actionProviderClass` and `android:actionProviderClass`. – CommonsWare May 06 '14 at 22:48
  • Is `menu.xml` the correct `menu` xml name? But your code refers `R.menu.main`, That means you have another `main.xml` under `res/menu` – Libin May 06 '14 at 22:49
  • @CommonsWare same result. Libin That's was an error when creating the question I have changed menu.xml to menu/main.xml – Charles-Eugene Loubao May 06 '14 at 23:07
  • Ok. See my answer. Let me if you see any issue – Libin May 06 '14 at 23:18
  • Are you sure that you actually have a Chromecast available to connect to? The action provider will not show up if there are no available matching routes. FWIW, [this sample project](https://github.com/commonsguy/cw-omnibus/tree/master/MediaRouter/ActionProvider) shows setting up a `MediaRouteActionProvider`, though not using Cast SDK-specific categories. – CommonsWare May 06 '14 at 23:44
  • where do you get the APP_ID ? – MindRoasterMir Apr 06 '19 at 13:48

3 Answers3

7

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. :)

Michal
  • 1,008
  • 1
  • 12
  • 16
6

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)

Charles-Eugene Loubao
  • 1,080
  • 2
  • 12
  • 22
  • THANK YOU. I've been trying 3 days to get the button to show. This code is clean and clear cut, worked in conjunction with the tutorials. – Andy Nov 24 '14 at 22:40
  • I am having similar issues. I had a working app, but then made a new receiver and replaced the Id in my strings.xml file. Now the icon doesn't show. I'm hoping I don't need to re-create my project... Any idea why copying code into a new project works? It can't be the only workaround – Mr Pablo Feb 05 '15 at 10:42
  • 1
    +MrPablo Check the Status of your new receiver on the Google Cast Developer Console . If it's Unpublished, Your Google Cast device must be registered for test otherwise you will not see the Cast Button – Charles-Eugene Loubao Feb 05 '15 at 12:24
  • Switching to using the deafult app_id (CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID) worked for me. Possibly because of Charles-Eugene Loubao's suggestion. – Lindsay-Needs-Sleep Jan 08 '18 at 10:35
  • what is this app_id ? from where do we get it ? – MindRoasterMir Apr 06 '19 at 13:48
2

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.

Vucko
  • 7,371
  • 2
  • 27
  • 45
  • 1
    Youtube shows the button in my case, but my app doesn't. – Oleg Yablokov Sep 23 '20 at 16:12
  • Then it must be something else for you, I just pointed what was the case for me. There's a dozen things that can go wrong. After fixing them all, I still couldn't see the button, so it was quite frustrating for me. – Vucko Sep 24 '20 at 07:53