HUAWEI Cast+ Kit是华为提供的以手机为中心的多屏协同能力。通过集成华为Cast+ Kit,可以实现手机与大屏类外备屏幕的快速、稳定、低时延协同, 带来多屏协同场景下的优质体验。
在这个Codelab中,你将创建Demo Project,并将Cast+ Kit的SDK集成到Demo Project中,以及完成Cast+ Kit服务的整体流程搭建。
集成Cast+ Kit能力,需要完成以下准备工作:
1.集成SDK。
请按照《Cast+ Kit SDK》获取依赖包,将下载好的aar包放到三方APP自身工程的libs文件夹下,同时在build.gradle添加依赖。
dependencies {
//添加依赖
implementation fileTree(include: ['*.aar'], dir: 'libs')
...
}
点击同步工程按钮,进行工程同步,如下图所示:
同步成功后,如下图所示:
2.在AndroidManifest.xml文件中添加权限。
在自身应用AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
3.初始化服务。
在MyService中初始化PlayerClient,处理Cast+服务回调消息,监听本地配置改变:
MyService.java
public class MyServie extends Service{
private PlayerClient mPlayerClient;
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate called().");
...
mCallbackHandler = new CallbackHandler(getMainLooper());
mPlayerClient = PlayerClient.getInstance();
mPlayerClient.registerCallback(mCallback);
mPlayerClient.init(mContext);
}
}
处理Cast+服务回调消息。
在接收到EVENT_ID_SERVICE_BIND_SUCCESS消息时,标识服务启动成功,在此消息下设置大屏端设备投屏能力、大屏端设备信息、鉴权模式信息;
在接收到EVENT_ID_PIN_CODE_SHOW消息时,标识有设备正在请求连接,大屏端提供界面供用户选择是否允许此次连接,若允许,通过该消息获取PIN码并展示,以供手机端输入PIN进行连接;
在接收到EVENT_ID_CONNECT_REQ消息时,大屏端跳转到投屏界面,该消息携带手机端设备信息DisplayInfo对象,开发者通过displayInfo.getProjectionDevice()获取手机端设备信息对象,请开发者妥善保管此对象,以在投屏业务中使用;
在接收到EVENT_ID_PAUSED消息时,大屏端可设置Surface对象并开始播放投屏视频流。MyService.java
public class MyServie extends Service{
...
private IEventListener mCallback = new IEventListener.Stub() {
public boolean onEvent(Event event) {
int eventId = event.getEventId();
Log.e(TAG, "eventId: " + eventId);
Message msg = mCallbackHandler.obtainMessage();
msg.what = eventId;
msg.obj = event;
msg.sendToTarget();
return true;
}
public boolean onDisplayEvent(int eventId, DisplayInfo displayInfo) {
Log.e(TAG, "handleDisplayEvent: " + eventId);
Message msg = mCallbackHandler.obtainMessage();
msg.what = eventId;
msg.obj = displayInfo;
msg.sendToTarget();
return true;
}
};
...
private class CallbackHandler extends Handler {
public CallbackHandler(Looper mainLooper) {
super(mainLooper);
}
@Override
public void handleMessage(Message msg) {
if (msg == null) return;
Log.d(TAG, "msg " + msg.what);
switch (msg.what) {
case Constant.EVENT_ID_SERVICE_BIND_SUCCESS:
if (mPlayerClient != null){
int framerate = 30;
HiSightCapability capability = new HiSightCapability(1920, 1080, 1920, 1080);
capability.setVideoFps(framerate);
mPlayerClient.setCapability(capability);
String deviceName = mBluetoothAdapter.getName();
DeviceInfo deviceInfo = new DeviceInfo(deviceName, DeviceInfo.TYPE_TV);
mPlayerClient.setDiscoverable(mIsDiscoverable, deviceInfo);
boolean needPassword = SharedPreferenceUtil.getAuthMode(mContext);
String password = SharedPreferenceUtil.getPassword(mContext);
boolean isNewPassword = false;
AuthInfo authInfo = null;
if (needPassword) {
Log.d(TAG, "password: $" + password + "$");
authInfo = new AuthInfo(AuthInfo.AUTH_MODE_PWD, password, isNewPassword);
} else {
authInfo = new AuthInfo(AuthInfo.AUTH_MODE_GENERIC);
}
mPlayerClient.setAuthMode(authInfo);
} else {
Log.e(TAG, "mPlayerClient is null.");
}
break;
case Constant.EVENT_ID_CONNECT_REQ:
DisplayInfo displayInfo = (DisplayInfo) msg.obj;
if (displayInfo != null) {
mProjectionDevice = displayInfo.getProjectionDevice();
startPlayActivity();
} else {
Log.e(TAG, "displayInfo is null.");
}
break;
case Constant.EVENT_ID_PIN_CODE_SHOW:
DisplayInfo displayInfo = (DisplayInfo) msg.obj;
if ((displayInfo != null) && (mProjectionDevice = displayInfo.getProjectionDevice()) != null) {
String pinCode = displayInfo.getPinCode();
String deviceName = mProjectionDevice.getDeviceName();
startAlertActivity(pinCode, deviceName);
} else {
Log.e(TAG, "displayInfo is null.");
}
break;
case Constant.EVENT_ID_PAUSED:
mPlayerClient.setHiSightSurface(mHiView.getHolder().getSurface());
mPlayerClient.play(new TrackControl(mProjectionDevice.getDeviceId()));
break;
}
}
}
...
}
参考《Cast+ Kit开发指南》调测验证方法,将手机端屏幕画面投屏到大屏界面。
干得好,你已经成功完成了Codelab并学到了:
您可以阅读下面链接,了解更多相关的信息。
相关文档