android.support.v7.app.MediaRouteButton does not display

这一生的挚爱 提交于 2019-12-07 09:30:22

问题


This is in my layout :

    <android.support.v7.app.MediaRouteButton
    android:id="@+id/button_fling"
    android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
    android:background="@drawable/mr_ic_media_route_holo_light"
    android:layout_height="wrap_content"       
    android:mediaRouteTypes="user"
    android:layout_weight="1"
    android:visibility="visible" />

and this in my my activity:

@Override

    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
            mMediaRouter = MediaRouter.getInstance(getApplicationContext());
            mMediaRouteSelector = new MediaRouteSelector.Builder()
            .addControlCategory(CastMediaControlIntent.categoryForCast(getString(R.string.app_id)))
            .build();
            mMediaRouterCallback = new MyMediaRouterCallback();

            mMediaRouteButton = (MediaRouteButton) findViewById(R.id.button_fling);
            mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
        button_fling2 = (ImageView)findViewById(R.id.button_fling2);
}



    public class MyMediaRouterCallback extends MediaRouter.Callback {
    public int mRouteCount =0;
    @Override
    public void onRouteAdded(MediaRouter router, RouteInfo route) {
        Log.d(TAG, "onRouteAdded");
        if (++mRouteCount == 1) {
            // Show the button when a device is discovered.
            Log.i(TAG,"MediaRoute is visible");
            button_fling2.setVisibility(View.VISIBLE);
            mMediaRouteButton.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRouteRemoved(MediaRouter router, RouteInfo route) {
        Log.d(TAG, "onRouteRemoved");
        if (--mRouteCount == 0) {
            // Hide the button if there are no devices discovered.
            Log.i(TAG,"MediaRoute is GONE");
            button_fling2.setVisibility(View.GONE);
            mMediaRouteButton.setVisibility(View.GONE);
        }
    }

}

ButtonFling2 is an ImageView I am using to test MyMediaRouterCallback is working or not. It successfully hides/shows the imageView. However for button_fling(which is a mediaRouteButton instance) does not show anything. It's as if it can't find the resources of the MediaRouteButton so it is showing no cast icon... anyone ever fix this or come across this?

I don't get any errors it just simply does not show, but the logs show that it is visible and the ImageView i have for testing shows up.


回答1:


AFter fiddling around with this thing, I was able to get the media Router button to show up. I am not entirely sure what I did but I did verify the appid and tried to use my whitelisted app id. I reinstalled the support library. THANK YOU ALL FOR WHO HELPED ON THIS!

I am not 100% sure how this happened but I hope that this helps someone figure out or at least get past this. ActionBar Activity and non action bar activities show the media router button now!!! :)




回答2:


Implementing onRouteChanged() callback instead of onRouteAdded() should fix the issue.

Example code:
    // for MediaRouterButtonActivity
        @Override
        public void onRouteChanged(MediaRouter router, RouteInfo route) {
          if (++mRouteCount == 1) {
                // Show the button when a device is discovered.
                mMediaRouteButton.setVisibility(View.VISIBLE);
          }
    }


来源:https://stackoverflow.com/questions/22366394/android-support-v7-app-mediaroutebutton-does-not-display

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