How Do I Discover A Chromecast Device Using Android?
I'd like to discover a Chromecast device and adjust the volume.
Solution 1:
- 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;
ContextapplicationContext= …; CastContextcastContext=newCastContext(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;
publicclassMyCastActivityextendsFragmentActivityimplementsMediaRouteAdapter {
private MediaRouteButton mMediaRouteButton;
private MediaRouter mMediaRouter;
private MediaRouteSelector mMediaRouteSelector;
private MediaRouter.Callback mMediaRouterCallback;
private CastDevice mSelectedDevice;
private MediaRouteStateChangeListener mRouteStateListener;
@OverrideprotectedvoidonCreate(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 = newMyMediaRouterCallback();
}
@OverrideprotectedvoidonStart() {
super.onStart();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}
@OverrideprotectedvoidonStop() {
mMediaRouter.removeCallback(mMediaRouterCallback);
super.onStop();
}
@OverrideprotectedvoidonDestroy() {
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.
privateclassMyMediaRouterCallbackextendsMediaRouter.Callback { @OverridepublicvoidonRouteSelected(MediaRouter router, RouteInfo route) { MediaRouteHelper.requestCastDeviceForRoute(route); } @OverridepublicvoidonRouteUnselected(MediaRouter router, RouteInfo route) { mSelectedDevice = null; mRouteStateListener = null; } } // MediaRouteAdapter implementation@OverridepublicvoidonDeviceAvailable(CastDevice device, MediaRouteStateChangeListener listener) { mSelectedDevice = device; mRouteStateListener = listener; } @OverridepublicvoidonSetVolume(double volume) { // Handle volume change. } @OverridepublicvoidonUpdateVolume(double delta) { // Handle volume change. }
}
Post a Comment for "How Do I Discover A Chromecast Device Using Android?"