15

我想找到一台 Chromecast 设备并调整音量。

4

1 回答 1

23
  1. 获取设备
  2. 将您的设备列入白名单(您需要设备序列号和 HTML5 接收器的 URL)
  3. 您将收到两个 APPID(开发/生产)
  4. 在您的开发环境中确保更新到 Android 支持库 v18
  5. 您将使用MediaRouter
  6. 初始化

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通过调用 构造 a MediaRouteHelper.buildMediaRouteSelector()。这种方法有两种形式:第一种不带参数,第二种采用接收者应用程序名称和/或消息协议列表。后一种形式用于执行与 SDK 的ApplicationSupportFilterListener.

mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST);
  • 将 MediaRouteSelector 分配给 MediaRouteButton。

mMediaRouteButton.setRouteSelector(mMediaRouteSelector);

  • 实现 aMediaRouter.Callback并将其添加到MediaRouter,传递CALLBACK_FLAG_REQUEST_DISCOVERYMediaRouter以启动发现。当用户在 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.CallbackonRouteSelected()回调中,调用 以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 回答