Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

html - Is it possible to programmatically connect to a chromecast route?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...