Reputation: 575
Is there any way to programmatically call the Cast Screen function in Android?
I do not want to develop a sender app, just want to start casting the screen extactly as in the Settings -> Display -> Cast Screen without going through those selections.
Disconnecting can be done prorammatically with the following code:
public void disconnect() {
MediaRouter mMediaRouter = (MediaRouter) getApplicationContext()
.getSystemService(Context.MEDIA_ROUTER_SERVICE);
mMediaRouter.selectRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO,
mMediaRouter.getDefaultRoute());
}
I need similar code to start casting.
Upvotes: 3
Views: 5807
Reputation: 1737
Screen Cast is a proprietary feature of the Google Cast app, and there is no public API for accessing it.
However, if you did want to develop your own sender app with identical functionality to Screen Cast, there is a library for accomplishing this, castscreen. You can import the module castscreen
, then make the activity you want to cast extend CastScreenActivity
. Add the Cast button to your menu with:
<item
android:id="@+id/media_route_menu_item"
android:title="Chromecast"
app:actionProviderClass="github.ankyl.castscreen.CastScreenMediaRouteActionProvider"
app:showAsAction="always" />
and set up screen casting by adding the following to your activity's onCreateOptionsMenu
:
MenuItem castButtonMenuItem = menu.findItem(R.id.media_route_menu_item);
super.prepareCastButton(castButtonMenuItem, YOUR_APP_ID);
Then a user can press the Cast button in that activity to mirror their whole screen to the receiver.
Upvotes: 2