gpt4 book ai didi

android - 如何发现使用 Android 的 Chromecast 设备?

转载 作者:可可西里 更新时间:2023-11-01 18:45:29 30 4
gpt4 key购买 nike

我想查找 Chromecast 设备并调整音量。

最佳答案

  1. 获取设备
  2. 获取您的 device whitelisted (您需要设备序列号和 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 接口(interface)的对象。

MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());

  • 通过调用构造一个 MediaRouteSelectorMediaRouteHelper.buildMediaRouteSelector()。有两种形式此方法:第一个不带参数,第二个带参数接收方应用程序名称和/或消息协议(protocol)列表。这个后一种形式用于执行等效于由 SDK 的 ApplicationSupportFilterListener 完成。

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

mMediaRouteButton.setRouteSelector(mMediaRouteSelector);

  • 实现一个MediaRouter.Callback并将其添加到MediaRouter,将 CALLBACK_FLAG_REQUEST_DISCOVERY 传递给 MediaRouter 以启动发现。当用户选择或取消选择路线时GUI picker,这个回调接口(interface)对应的方法会被调用。
    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.
}

}

关于android - 如何发现使用 Android 的 Chromecast 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17842845/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com