实现一个简单的投屏影视功能
- 作者:
- 淡白
- 创建时间:
- 2020-03-30 13:52:54
- java
摘要:本文介绍了如何给影视app添加一个投屏功能。首先需要在项目中添加依赖,然后在Activity中实现投屏功能。具体实现包括搜索设备、添加设备到下拉框、设置投屏信息以及实现投屏按钮的点击事件。最后给出了参考地址和项目地址供参考。
给影视app添加个投屏功能
手机app上选中影视投屏给电视. app地址
添加依赖
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
maven {url 'http://4thline.org/m2'}
}
}
android {
//必须添加,不加会报错
packagingOptions {
exclude 'META-INF/beans.xml'
}
}
dependencies {
compileOnly 'javax.enterprise:cdi-api:2.0'
implementation 'com.github.yanbo469:VideoDlnaScreen:v1.0'
}
在Activity中实现
代码地址 我在Activity布局xml中添加了一个下拉框和一个按钮
在onCreate中注册服务和获得组件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ys);
spinner=findViewById(R.id.spinner);
touping=findViewById(R.id.touping);
//使用前先注册服务
ClingManager.getInstance().startClingService();
tp();
}
投屏初始化和实现
private void tp(){
//搜索设备
clingDevices = DeviceManager.getInstance().getClingDeviceList();
adapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item);
//设置下拉列表风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将适配器添加到spinner中去
spinner.setAdapter(adapter);
spinner.setVisibility(View.VISIBLE);//设置默认显示
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//遍历获取当前选中的设备
for (int i=0;i<clingDevices.size();i++){
if(getDname(clingDevices.get(i)).equals(((TextView)arg1).getText())){
DeviceManager.getInstance().setCurrClingDevice(clingDevices.get(i));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//添加设备到下拉框
if(clingDevices.size()>0){
for (int i=0;i<clingDevices.size();i++){
adapter.add(getDname(clingDevices.get(i)));
}
}
//投屏按钮点击事件
touping.setOnClickListener(b->{
//设置网络投屏的信息
RemoteItem itemurl = new RemoteItem("淡白影视", "996", "淡白影视",107362668, "00:00:00", "1920x1080",jiList.get(playindex).getUrl());
//添加网络投屏的信息
ClingManager.getInstance().setRemoteItem(itemurl);
remoteItem = ClingManager.getInstance().getRemoteItem();
//播放
if (ControlManager.getInstance().getState() == ControlManager.CastState.STOPED) {
newPlayCastRemoteContent();
}
});
}
/**
* 网络投屏
*/
private String getDname(ClingDevice device){
return device.getDevice().getDetails().getFriendlyName()+"-"+device.getDevice().getDetails().getPresentationURI().getHost();
}
private void newPlayCastRemoteContent() {
ControlManager.getInstance().setState(ControlManager.CastState.TRANSITIONING);
ControlManager.getInstance().newPlayCast(remoteItem, new ControlCallback() {
@Override
public void onSuccess() {
ControlManager.getInstance().setState(ControlManager.CastState.PLAYING);
ControlManager.getInstance().initScreenCastCallback();
Log.d("投屏", "投屏成功");
}
@Override
public void onError(int code, String msg) {
ControlManager.getInstance().setState(ControlManager.CastState.STOPED);
Log.d("投屏", "投屏失败");
}
});
}