1

So I modified the Mediarouter source to be dependent on ActionBarSherlock rather than AppCompat. However, now the callback from MediaRouteAdapter onDeviceAvailable(...) is not being called.

Any ideas on how to get this to work?

I don't want to leave the cast button always visible. Rather, I want to use onDeviceAvailable(..) to only make it visible when a device is available.

Prem
  • 3,373
  • 7
  • 29
  • 43
  • Where did you pull the source from for the Mediarouter? – Steve Oct 24 '13 at 14:15
  • https://android.googlesource.com/ – Prem Oct 24 '13 at 17:51
  • Thanks. Would you be able to share the steps you took to get to the point where you are now? I'm a bit lost where to start, and you seem to be the only one out there having any luck getting the two to play nice. – Steve Oct 24 '13 at 18:46
  • I saved the mediaRouter folder, and added it as a submodule to my project. I added ActionBarSherlock as a dependency to mediaRouter, and the latest version of the v4 android support library. Now when you try to compile you may find a few (3-5) errors, from references to AppCompat resources. Basically rename those resources to ABS resources. For most of them, just replacing the part of the name that says 'AppCompat' with 'Sherlock' works. – Prem Oct 25 '13 at 19:44
  • Thanks. What mediaRouter folder? I don't see any code under frameworks/support/v7/mediarouter. – Steve Oct 25 '13 at 19:52
  • My bad, this is where the support libraries are: https://github.com/android/platform_frameworks_support – Prem Oct 25 '13 at 20:27
  • Hi Prem, I tried following these steps, but the mediarouter submodule won't compile. There seems to be more errors than just AppCompat references. WHen you say submodule what exactly do you mean? I simply added the mediarouter folder as an "existing android project" into my Eclipse workspace. Is that what you mean? – b.lyte Oct 29 '13 at 01:07
  • Hi @clu the instructions I provided are towards modules in IntelliJ IDEA. By submodule I mean a module dependency. I don't use Eclipse much but I think it means adding the module as a library to which your app is dependent on. – Prem Oct 29 '13 at 12:31

1 Answers1

2

I found a hack on how to fix this.

If you use appcompat, the cast icon will only be visible when you have a chromecast device in your network, based upon your implementation of onDeviceAvailable(...).

However, if you use ABS, the onDeviceAvailable(...) method is not called when a chromecast device is available on the network. So in order to see the cast icon, we must set it as always visible.

So the issue is that the cast icon is always visible (when you set visibility to visible or when you add it to your actionbar).

HOWEVER: The MediaRouter has different view states. One for when a cast device is available and one for when there isn't one available.

The Mediarouter library consists of drawable pngs that are the icons for when a device is available or not.

Just replace the one for which 'no route is available' with a blank resource, or anything that suits the app.

When a device is available, the correct cast icon will show, and when not available your custom resource will show (blank if the resource is a blank view).

Here are full details on how to obtain the mediarouter source code, and how to fix this: http://www.droidorbit.com/2013/10/mediarouter-and-actionbarsherlock.html

Prem
  • 3,373
  • 7
  • 29
  • 43
  • I followed the link you suggested above. But I got the error: MediaRouterJellybean, MediaRouterJellybeanMr1 cannot be resolved to a type in SystemMediaRouteProvider.java file. And R.attr.isLightTheme cannot be resolved or is not a field in MediaRouterThemeHelper.java file. Currently I commented these code for error free, then integrate Cast sample with ABS and display cast icon in ActionBar. But I got the error: java.lang.ClassCastException: android.support.v7.app.MediaRouteActionProvider cannot be cast to com.actionbarsherlock.view.ActionProvider. Please help me to solve it. Thanks – Huy Duong Tu Oct 30 '13 at 09:13
  • Could you give your modified MediaRouter source code? My email is duongtuhuybk@gmail.com. Thanks in advance – Huy Duong Tu Oct 30 '13 at 09:17
  • email sent to you Huy Dong Tu – Prem Oct 30 '13 at 14:56
  • @Prem Thanks to your blog I managed to implement the MediaRouter successfully! Thanks! The only thing is that for me it works fine without any hacks, the ChromeCast icon appears/disappears automatically depending on whether or not the ChromeCast is switched on or not. I used the answer to this question http://stackoverflow.com/a/17842846/1433482 and now my onDeviceAvailable is called too. Thought it might be of interest to you... Also, my icon is a menu item, like described here http://androiddev.orkitra.com/?p=70675 – roarster Dec 27 '13 at 15:21
  • This solution doesn't work anymore, `MediaRouteAdapter` was removed from the API. I have the same issue, I want to hide the icon if there's no cast device in the network. Any ideas how to do that? – Nimrod Dayan Apr 27 '15 at 12:56