Is it possible to programmatically connect to a chromecast route?

早过忘川 提交于 2019-11-27 08:26:57

问题


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:

  1. Get an instance of the MediaRouter singleton from the system: mMediaRouter
  2. Build a selector: mMediaRouteSelector = new MediaRouteSelector.Builder() .addControlCategory( CastMediaControlIntent .categoryForCast(YOUR_APP_ID)).build();
  3. Add a callback to initiate scan: mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
  4. The onRouteAdded() and onRouteRemoved() 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.
  5. You can select a route by calling mMediaRouter.selectRoute(aRouteInfo). Then the onRouteSelected() 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!