Cast Button未显示

时间:2014-05-06 22:45:20

标签: java android chromecast

我正在尝试使用Android Studio创建ChomeCast发件人应用。我已经实现了所有代码都需要能够添加Cast按钮但它不可用

这是我的代码。

的build.gradle

    apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.google.android.gms:play-services:4.3.+'
    compile 'com.android.support:mediarouter-v7:19.+'



}

RES /菜单/ main.xml中

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.castbuttontest.app.MainActivity" >


    <item
        android:id="@+id/media_route_menu_item"
        android:title="@string/media_route_menu_title"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        app:showAsAction="always"/>
</menu>

MainActivity.java

    package com.example.castbuttontest.app;

    import android.support.v4.view.MenuItemCompat;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.support.v7.app.MediaRouteActionProvider;
    import android.support.v7.media.MediaRouteSelector;
    import android.support.v7.media.MediaRouter;
    import android.view.Menu;
    import android.view.MenuItem;

import com.google.android.gms.cast.Cast;
import com.google.android.gms.cast.CastDevice;
import com.google.android.gms.cast.CastMediaControlIntent;
import com.google.android.gms.common.api.GoogleApiClient;


public class MainActivity extends ActionBarActivity {


    MediaRouter mediaRouter;
    MediaRouteSelector mediaRouteSelector;
    MediaRouter.Callback myMediaRouterCallBack = new MediaRouter.Callback() {
        @Override
        public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
            CastDevice device = CastDevice.getFromBundle(route.getExtras());

        }

        @Override
        public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String APP_ID = "F6D3E50B";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaRouter = MediaRouter.getInstance(this);
        mediaRouteSelector = new MediaRouteSelector.Builder()
                //.addControlCategory(CastMediaControlIntent.categoryForCast(APP_ID))
                .addControlCategory(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mediaRouter.addCallback(mediaRouteSelector, myMediaRouterCallBack,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
    }

    @Override
    protected void onStop() {
        mediaRouter.removeCallback(myMediaRouterCallBack);
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem mediaRouterItem = menu.findItem(R.id.media_route_menu_item);
        MediaRouteActionProvider provider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouterItem);
        provider.setRouteSelector(mediaRouteSelector);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return super.onOptionsItemSelected(item);
    }

}

你能说出问题所在吗? 在此先感谢

编辑: 我终于解决了这个问题。我创建了一个新的Empty项目,并按照Sender App教程(Link)重新编写代码:

MainActivity.java

    /*
   Copyright 2014 Charles-Eugene LOUBAO

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package cf.charly1811.android.sample.MediaRouteProvider;

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.cast.CastDevice;
import com.google.android.gms.cast.CastMediaControlIntent;


public class MainActivity extends ActionBarActivity {
    /**
     * This sample shows how to add the Cast Button to the action bar using MediaRouterProvider
     * @see "https://developers.google.com/cast/docs/android_sender"
     */
    public static final String TAG = MainActivity.class.getSimpleName();
    String APP_ID = "F6D3E50B";

    MediaRouter mediaRouter;
    MediaRouteSelector mediaRouteSelector;
    MediaRouter.Callback mMediaRouterCallback;
    CastDevice mCastDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaRouter = MediaRouter.getInstance(this);
        mediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory(CastMediaControlIntent.categoryForCast(APP_ID))
                .build();

        mMediaRouterCallback = new MediaRouter.Callback() {
            @Override
            public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
                super.onRouteSelected(router, route);
                Log.d(TAG, "Connected to "+ route.getName());
                mCastDevice = CastDevice.getFromBundle(route.getExtras());
                Toast.makeText(getApplicationContext(), "Connected to "+route.getName(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {
                super.onRouteUnselected(router, route);
                mCastDevice = null;
            }
        };
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        mediaRouter.addCallback(mediaRouteSelector,mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
        if(isFinishing())
        {
            mediaRouter.removeCallback(mMediaRouterCallback);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem media_route_menu_item = menu.findItem(R.id.media_route_menu_item);
        MediaRouteActionProvider provider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(media_route_menu_item);
        provider.setRouteSelector(mediaRouteSelector);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        /*
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        */
        return super.onOptionsItemSelected(item);
    }
}

现在它正常运作。图标现已可用,我可以连接到我的Chromecast。我不确定,但我认为问题是关于MediaRouter.Callback的实例;

PS:我已经在GitHub上传了完整的代码。你可以看看。 (Link

3 个答案:

答案 0 :(得分:7)

从头开始重写它并不能真正向任何人显示问题所在......

你有

.addControlCategory(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)

而不是

.addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))

您将APP_ID直接传递到类别中。我猜类别有特殊的格式,而APP_ID并不是这样。我犯了同样的错误。 :)

答案 1 :(得分:6)

我终于解决了这个问题。我创建了一个新的Empty项目,并按照Sender App教程(Link)重新编写代码:

MainActivity.java

    /*
   Copyright 2014 Charles-Eugene LOUBAO

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package cf.charly1811.android.sample.MediaRouteProvider;

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.cast.CastDevice;
import com.google.android.gms.cast.CastMediaControlIntent;


public class MainActivity extends ActionBarActivity {
    /**
     * This sample shows how to add the Cast Button to the action bar using MediaRouterProvider
     * @see "https://developers.google.com/cast/docs/android_sender"
     */
    public static final String TAG = MainActivity.class.getSimpleName();
    String APP_ID = "F6D3E50B";

    MediaRouter mediaRouter;
    MediaRouteSelector mediaRouteSelector;
    MediaRouter.Callback mMediaRouterCallback;
    CastDevice mCastDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaRouter = MediaRouter.getInstance(this);
        mediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory(CastMediaControlIntent.categoryForCast(APP_ID))
                .build();

        mMediaRouterCallback = new MediaRouter.Callback() {
            @Override
            public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
                super.onRouteSelected(router, route);
                Log.d(TAG, "Connected to "+ route.getName());
                mCastDevice = CastDevice.getFromBundle(route.getExtras());
                Toast.makeText(getApplicationContext(), "Connected to "+route.getName(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {
                super.onRouteUnselected(router, route);
                mCastDevice = null;
            }
        };
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        mediaRouter.addCallback(mediaRouteSelector,mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
        if(isFinishing())
        {
            mediaRouter.removeCallback(mMediaRouterCallback);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem media_route_menu_item = menu.findItem(R.id.media_route_menu_item);
        MediaRouteActionProvider provider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(media_route_menu_item);
        provider.setRouteSelector(mediaRouteSelector);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        /*
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        */
        return super.onOptionsItemSelected(item);
    }
}

现在它正常运作。图标现已可用,我可以连接到我的Chromecast。我不确定,但我认为问题是关于MediaRouter.Callback的实例;

PS:我已经在GitHub上传了完整的代码。你可以看看。 (Link

答案 2 :(得分:1)

我一直在努力寻找解决方案,而我找不到任何帮助。直到我最终找到this link

  

A 不可用:虽然Cast接收器不可用,但Cast按钮不会出现

检查附近是否有 CAST TO 的设备。我没有,所以按钮被设计隐藏了。您可以轻松地检查,转到youtube应用程序,并且当没有接收器应用程序存在时,那里没有强制转换按钮。希望这可以节省一些时间,因为我很难找到它为什么不出现。