我想找到一台 Chromecast 设备并调整音量。
问问题
7264 次
1 回答
23
- 获取设备
- 将您的设备列入白名单(您需要设备序列号和 HTML5 接收器的 URL)
- 您将收到两个 APPID(开发/生产)
- 在您的开发环境中确保更新到 Android 支持库 v18
- 您将使用MediaRouter
- 初始化
import com.google.cast.CastContext;
Context applicationContext = …; CastContext castContext = new
CastContext(applicationContext);
- 你需要一个MediaRouteButton
< android.support.v7.app.MediaRouteButton
android:id="@+id/media_route_button"
android:mediaRouteTypes="user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
import com.google.cast.CastContext;
import com.google.cast.CastDevice;
import com.google.cast.MediaRouteAdapter;
import com.google.cast.MediaRouteHelper;
import com.google.cast.MediaRouteStateChangeListener;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.MediaRouteButton;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.support.v7.media.MediaRouter.RouteInfo;
public class MyCastActivity extends FragmentActivity implements MediaRouteAdapter {
private MediaRouteButton mMediaRouteButton;
private MediaRouter mMediaRouter;
private MediaRouteSelector mMediaRouteSelector;
private MediaRouter.Callback mMediaRouterCallback;
private CastDevice mSelectedDevice;
private MediaRouteStateChangeListener mRouteStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_cast_activity);
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
- 构造一个
CastContext
.
mCastContext = new CastContext(getApplicationContext());
-MinimalCastMediaRouteProvider
通过调用
MediaRouteHelper.registerMinimalMediaRouteProvider
()注册,传递一个实现MediaRouteAdapter
接口的对象。
MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
MediaRouteSelector
通过调用 构造 aMediaRouteHelper.buildMediaRouteSelector()
。这种方法有两种形式:第一种不带参数,第二种采用接收者应用程序名称和/或消息协议列表。后一种形式用于执行与 SDK 的ApplicationSupportFilterListener
.
mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST);
- 将 MediaRouteSelector 分配给 MediaRouteButton。
mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
- 实现 a
MediaRouter.Callback
并将其添加到MediaRouter
,传递CALLBACK_FLAG_REQUEST_DISCOVERY
给MediaRouter
以启动发现。当用户在 GUI 选择器中选择或取消选择路由时,将调用此回调接口上的相应方法。
mMediaRouterCallback = new MyMediaRouterCallback(); } @Override protected void onStart() { super.onStart(); mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); } @Override protected void onStop() { mMediaRouter.removeCallback(mMediaRouterCallback); super.onStop(); } @Override protected void onDestroy() { MediaRouteHelper.unregisterMediaRouteProvider(mCastContext); mCastContext.dispose(); super.onDestroy(); }
- 在
MediaRouter.Callback
的onRouteSelected()
回调中,调用 以MediaRouteHelper.requestCastDeviceForRoute()
获取所选媒体路由的 CastDevice 对象,以及MediaRouteStateChangeListener
每当路由音量或连接状态发生变化时需要通知的对象。
private class MyMediaRouterCallback extends MediaRouter.Callback { @Override public void onRouteSelected(MediaRouter router, RouteInfo route) { MediaRouteHelper.requestCastDeviceForRoute(route); } @Override public void onRouteUnselected(MediaRouter router, RouteInfo route) { mSelectedDevice = null; mRouteStateListener = null; } } // MediaRouteAdapter implementation @Override public void onDeviceAvailable(CastDevice device, MediaRouteStateChangeListener listener) { mSelectedDevice = device; mRouteStateListener = listener; } @Override public void onSetVolume(double volume) { // Handle volume change. } @Override public void onUpdateVolume(double delta) { // Handle volume change. }
}
于 2013-07-24T19:11:44.357 回答