简体   繁体   中英

cast device discovery won't work with my application ID

I'm very new to the google cast sdk and I have registered a app on the cast developer console with a style media receiver. My idea is to send images to the chromecast device from an android application. I have tried a few examples and tried writing my own now and following is the code snippets I have used to get the device discovered. but it doesn't seem to work. I have set the correct permissions in the manifest as well but I can't get the cast button to display on the action bar for some reason.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(
            android.R.color.transparent));


    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    mMediaRouteSelector = new MediaRouteSelector.Builder()
    //.addControlCategory(
    //CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID).build();

    .addControlCategory(
            CastMediaControlIntent.categoryForCast(getResources()
                    .getString(R.string.app_id))).build();

    mMediaRouterCallback = new MyMediaRouterCallback();
}

@Override
protected void onResume() {
    super.onResume();
    // Start media router discovery
    mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
            MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}       

@Override
protected void onPause() {
    if (isFinishing()) {
        // End media router discovery
        mMediaRouter.removeCallback(mMediaRouterCallback);
    }
    super.onPause();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
    MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat
            .getActionProvider(mediaRouteMenuItem);
    // Set the MediaRouteActionProvider selector for device discovery.
    mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
    return true;
}

If using DEFAULT_MEDIA_RECEIVER_APPLICATION_ID as your appId makes the cast button visible but using your own appId doesn't, then it means your cast device is not registered correctly for your application. If that is the case, you need to make sure the correct serial number has been used in the developer console and you have the "send serial number ..." checkbox checked when you set up your chromecast. After doing all of this, reboot your chromecast so it can pick up the new configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM