我想发现一个Chromecast设备并调整音量。
最佳答案
import com.google.cast.CastContext;
Context applicationContext = …; CastContext castContext = new
CastContext(applicationContext);
< 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
接口(interface)的对象。MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
MediaRouteSelector
MediaRouteHelper.buildMediaRouteSelector()
。有两种形式此方法:第一个不带参数,第二个不带参数
接收器应用程序名称和/或消息协议(protocol)列表。这个
后一种形式用于执行与之等效的设备过滤
由SDK的
ApplicationSupportFilterListener
完成。 mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST);
mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
MediaRouter.Callback
并将其添加到MediaRouter
中,将
CALLBACK_FLAG_REQUEST_DISCOVERY
传递到MediaRouter
来发起发现。当用户选择或取消选择路线时
GUI选择器,此回调接口(interface)上的相应方法将
被调用。
MediaRouter.Callback
的 onRouteSelected()
回调中,拨打电话到
MediaRouteHelper.requestCastDeviceForRoute()
以获取所选媒体路线的CastDevice对象,以及
MediaRouteStateChangeListener
,无论何时需要通知路由量或连接状态更改。