问题
I'm writing a chromecast receiver application that will (hopefully) allow me to remotely put alert messages up on my TV to serve as reminders.
My plan was to have a dedicated wireless device on my home network that would constantly poll for new messages from a centralized server. When a new message was found, it would connect to a chromecast route, turning on the TV and displaying the new message.
But as far as I can tell, the only way to activate a chromecast route is by manually clicking the chromecast icon on my Chrome browser or wireless device.
Is there a way, programmatically, to activate the chromecast? Can it be done in the sender?
回答1:
You can programmatically scan for cast devices and connect to them if needed. Steps are:
- Get an instance of the MediaRouter singleton from the system:
mMediaRouter
- Build a selector:
mMediaRouteSelector = new MediaRouteSelector.Builder() .addControlCategory( CastMediaControlIntent .categoryForCast(YOUR_APP_ID)).build();
- Add a callback to initiate scan:
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
- The
onRouteAdded()
andonRouteRemoved()
of your callback (i.e.mMediaRouterCallback
) will be called as routes are discovered or removed. You can maintain a list of routes in your app and keep them up to date by using these two callbacks. - You can select a route by calling
mMediaRouter.selectRoute(aRouteInfo)
. Then theonRouteSelected()
of your callback will be called and you can extract the cast device as usual and do as you please.
These said, remember that if you want to show a notification to users on TV your app should be running on the chromecast at the time you want to send the notification.
来源:https://stackoverflow.com/questions/21706041/is-it-possible-to-programmatically-connect-to-a-chromecast-route