投屏功能接入
更新时间:2023-03-17
接入准备
接入投屏功能,需要使用播放器SDK高级版,并申请高级版License。
功能介绍
在高级版SDK中,提供了DLNA投屏能力,可以将手机端的视频内容推送到大屏端进行播放,并且支持在手机端远程控制大屏端的媒体播放,该能力由ProjectionWrapper组件提供,请确保该组件已集成到你的App中。
快速开始
1.初始化ProjectionEngine组件,传入LicenseID和设备发现回调
// 需要传入您申请的高级版证书LicenseID,ID可以在百度智能云控制台查看
ProjectionEngine.setLicenseID(BaseApp.APP_ID);
// 设备发现回调,可以在这里将发现到的接收端信息保存起来
private ArrayList<DeviceUpnp> mCastReceivers = new ArrayList<>();
private DeviceUpnp.Devicelistener upnpListenerImp = new DeviceUpnp.Devicelistener() {
@Override
public void OnAddDevice(DeviceUpnp device) {
Log.d(TAG, "onAddDevice " + device.getUuid());
mCastReceivers.add(device);
}
@Override
public void OnRemoveDevice(DeviceUpnp device) {
}
};
ProjectionEngine.getInstance().setUpnpDeviceServerListener(getContext(), upnpListenerImp);
2.开始搜索投屏设备,当找到设备时,会利用上面的回调进行通知
ProjectionEngine.getInstance().searchDeviceBegin();
3.选择设备并发起投屏
// 创建渲染控制器回调,当投屏开始后,会通知接收端的状态变化
private ProjectionRenderListenerImp projectionRenderListenerImp = new ProjectionRenderListenerImp();
class ProjectionRenderListenerImp implements ProjectionRender.ProjectionRenderListener {
@Override
public void onSetAVTransportURI(int renderId, boolean result) {
Log.i(TAG, "ProjectionRender onSetAVTransportURI " + renderId + ", result: " + result);
}
@Override
public void onPaused(int renderId, boolean result) {
Log.i(TAG, "ProjectionRender onPaused " + renderId + ", result " + result);
}
public ProjectionRenderListenerImp() {
}
@Override
public void onDuration(int renderId, final Integer duration) {
Log.i(TAG, "ProjectionRender onDuration " + renderId + ", duration " + duration);
}
@Override
public void onCurrent(int renderId, final Integer current) {
Log.i(TAG, "ProjectionRender onCurrentPosition " + renderId + ", position " + current);
}
@Override
public void onPlayed(int renderId, boolean result) {
Log.i(TAG, "ProjectionRender onPlayed " + renderId + ", result " + result);
}
@Override
public void onStoped(int renderId, boolean result) {
Log.i(TAG, "ProjectionRender onStoped " + renderId + ", result " + result);
}
}
// 从之前保存的接收端信息中选出一个作为发起投屏的目标
DeviceUpnp device = mCastReceivers.get(position);
private int mCastReceiverIdx = -1;
if (null != device) {
// 分配渲染控制器, mCastReceiverIdx作为渲染控制器标识,后续的媒体播放控制都基于它完成
mCastReceiverIdx = ProjectionEngine.getInstance().distributeProjectionRender(device, projectionRenderListenerImp);
// 设置接收端要播放的媒体url
ProjectionEngine.getInstance().setAVTransportURL(mCastReceiverIdx, url);
// 开始播放
ProjectionEngine.getInstance().play(mCastReceiverIdx, 1);
}
4.控制接收端的媒体播放
// 播放
ProjectionEngine.getInstance().play(mCastReceiverIdx, 1);
// 暂停
ProjectionEngine.getInstance().pause(mCastReceiverIdx);
// seek, 单位为秒
ProjectionEngine.getInstance().seek(mCastReceiverIdx, 30);
// 停止
ProjectionEngine.getInstance().stop(mCastReceiverIdx);
5.停止搜索设备
ProjectionEngine.getInstance().searchDeviceEnd();
6.结束投屏并释放渲染控制器
ProjectionEngine.getInstance().stop(mCastReceiverIdx);
ProjectionEngine.getInstance().releaseProjectionRender(mCastReceiverIdx);
在播放器SDK Demo中的AdvancedMediaController类对上述流程有详细的展示,可以参考。
接口说明
ProjectionEngine类
接口名 | 描述 |
---|---|
ProjectionEngine getInstance() | 获取投屏引擎单例 |
void setLicenseID(String id) | 传入LicenseID |
String getVersion() | 获取投屏引擎版本号 |
void setUpnpDeviceServerListener(Context context, DeviceUpnp.Devicelistener upnpListener) | 设置设备发现回调 |
void searchDeviceBegin() | 开始搜索设备,鉴权失败的情况下会抛出异常 |
void searchDeviceEnd() | 停止搜索设备 |
int distributeProjectionRender(DeviceUpnp device, ProjectionRender.ProjectionRenderListener renderListener) | 分配渲染控制器,返回值作为渲染控制器标识 |
void releaseProjectionRender(Integer renderIndex) | 释放渲染控制器 |
void setAVTransportURL(Integer renderIndex, String url) | 设置接收端播放的URL |
void play(Integer renderIndex, Integer speed) | 开始接收端播放,当前speed仅支持传入1 |
void pause(Integer renderIndex) | 暂停接收端播放 |
void stop(Integer renderIndex) | 停止接收端播放 |
void seek(Integer renderIndex, Integer realTime) | 控制接收端进度seek,单位为秒 |
Integer getDuration(Integer renderIndex) | 获取媒体流时长 |
Integer getCurPos(Integer renderIndex) | 获取当前播放位置 |
DeviceUpnp类
接口名 | 描述 |
---|---|
String getUuid() | 获取接收端设备ID |
String getFriendlName() | 获取接收端设备名 |
事件回调 | 含义 |
---|---|
void OnAddDevice(DeviceUpnp device) | 发现设备 |
void OnRemoveDevice(DeviceUpnp device) | 移除设备 |