I'd like to discover a Chromecast device and adjust the volume.
Asked
Active
Viewed 7,973 times
1 Answers
23
- Get a device
- Get your device whitelisted (you'll need the device serial #, and a URL for your HTML5 receiver)
- You'll be sent two APPID (development / production)
- In your development environment make sure to update to Android Support Library v18
- You'll be using MediaRouter
- Initialize
import com.google.cast.CastContext;
Context applicationContext = …; CastContext castContext = new
CastContext(applicationContext);
- You'll need a MediaRouteButton
< android.support.v7.app.MediaRouteButton
android:id="@+id/media_route_button"
android:mediaRouteTypes="user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
import com.google.cast.CastContext;
import com.google.cast.CastDevice;
import com.google.cast.MediaRouteAdapter;
import com.google.cast.MediaRouteHelper;
import com.google.cast.MediaRouteStateChangeListener;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.MediaRouteButton;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.support.v7.media.MediaRouter.RouteInfo;
public class MyCastActivity extends FragmentActivity implements MediaRouteAdapter {
private MediaRouteButton mMediaRouteButton;
private MediaRouter mMediaRouter;
private MediaRouteSelector mMediaRouteSelector;
private MediaRouter.Callback mMediaRouterCallback;
private CastDevice mSelectedDevice;
private MediaRouteStateChangeListener mRouteStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_cast_activity);
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
- Construct a
CastContext
.
mCastContext = new CastContext(getApplicationContext());
- Register the MinimalCastMediaRouteProvider
by calling
MediaRouteHelper.registerMinimalMediaRouteProvider
(), passing an
object that implements the MediaRouteAdapter
interface.
MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
- Construct a
MediaRouteSelector
by callingMediaRouteHelper.buildMediaRouteSelector()
. There are two forms of this method: the first takes no parameters and the second takes a receiver application name and/or a list of message protocols. This latter form is used to perform device filtering equivalent to that done by the SDK’sApplicationSupportFilterListener
.
mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST);
- Assign the MediaRouteSelector to the MediaRouteButton.
mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
- Implement a
MediaRouter.Callback
and add it to theMediaRouter
, passingCALLBACK_FLAG_REQUEST_DISCOVERY
to theMediaRouter
to initiate discovery. When the user selects or deselects a route in the GUI picker, the corresponding method on this callback interface will be invoked.
mMediaRouterCallback = new MyMediaRouterCallback(); } @Override protected void onStart() { super.onStart(); mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); } @Override protected void onStop() { mMediaRouter.removeCallback(mMediaRouterCallback); super.onStop(); } @Override protected void onDestroy() { MediaRouteHelper.unregisterMediaRouteProvider(mCastContext); mCastContext.dispose(); super.onDestroy(); }
- In the
MediaRouter.Callback
’sonRouteSelected()
callback, make a call toMediaRouteHelper.requestCastDeviceForRoute()
to obtain a CastDevice object for the selected media route, as well as theMediaRouteStateChangeListener
that will need to be notified whenever route volume or connecting state changes.
private class MyMediaRouterCallback extends MediaRouter.Callback { @Override public void onRouteSelected(MediaRouter router, RouteInfo route) { MediaRouteHelper.requestCastDeviceForRoute(route); } @Override public void onRouteUnselected(MediaRouter router, RouteInfo route) { mSelectedDevice = null; mRouteStateListener = null; } } // MediaRouteAdapter implementation @Override public void onDeviceAvailable(CastDevice device, MediaRouteStateChangeListener listener) { mSelectedDevice = device; mRouteStateListener = listener; } @Override public void onSetVolume(double volume) { // Handle volume change. } @Override public void onUpdateVolume(double delta) { // Handle volume change. }
}

kolistivra
- 4,229
- 9
- 45
- 58

Les Vogel - Google DevRel
- 7,092
- 1
- 25
- 39
-
Can I discover a device without using v18 libraries, and modifying the mediarouter source to make it dependent on ActionBarSherlock rather than AppCompat? So far it hasn't worked, but I'm wondering if there is something else missing. – Prem Oct 21 '13 at 20:26
-
Nope - It doesn't work w/ ActionBarSherlock - at least last time I checked it. – Les Vogel - Google DevRel Oct 21 '13 at 20:58
-
I have follow all these steps... but still unable to cast on TV... Device is not found... I have confusion.... I am working on sender app so should I need reciver device whitelisted and where I have to put App ID if I whitelist my device... kindly kindly kindly help me out. stuck on it more than 2 days. @LesVogel-GoogleDevRel – pareshy Nov 14 '13 at 10:48
-
1In Setup, did you [x] send your serial # to google. You can test by
:9222 – Les Vogel - Google DevRel Nov 14 '13 at 22:31 -
@LesVogel-GoogleDevRel... could u please guide me what should the url file contain?? – pareshy Nov 19 '13 at 10:19
-
The URL for your Receiver is usually something like mysite.com/cast/receiver.html -- A HTML5 application that plays your content. The load url should be the content you want played on the Chromecast – Les Vogel - Google DevRel Nov 19 '13 at 20:47
-
Is this still accurate for the official Cast SDK? The `CastContext` class seems to have vanished. – Brian White Jun 21 '14 at 16:00
-
This is old, the v2 SDK replaces it. – Les Vogel - Google DevRel Jun 22 '14 at 01:25