I'd like to discover a Chromecast device and adjust the volume.
If Chromecast isn't showing up in your Google Home app first ensure that your device is plugged in and powered on. Then, make sure your Chromecast and Google Home are both connected to the same network. Lastly, make sure you've given the Google Home app the right location permissions on your mobile device.
import com.google.cast.CastContext;
Context applicationContext = …; CastContext castContext = new
CastContext(applicationContext);
< 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);
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());
MediaRouteSelector
by calling
MediaRouteHelper.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’s ApplicationSupportFilterListener
.mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST);
mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
MediaRouter.Callback
and add it to the MediaRouter
,
passing CALLBACK_FLAG_REQUEST_DISCOVERY
to the MediaRouter
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(); }
MediaRouter.Callback
’s onRouteSelected()
callback, make a call
to MediaRouteHelper.requestCastDeviceForRoute()
to obtain a
CastDevice object for the selected media route, as well as the
MediaRouteStateChangeListener
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. }
}
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