Android-再次解读萤石云视频
程序员文章站
2023-11-21 23:02:58
### 前言 我之前写过一篇萤石云的集成文章,很多人问我有没有demo, 今天我再次总结一下, 并加个些功能。 - 集成步骤 - 视频预览播放 - 视频放大缩小 - 视频的质量切换 - 截图 - 视频加载细节 之前的文章大家可以看下面的链接: https://mp.weixin.qq.com/s/V ......
### 前言
我之前写过一篇萤石云的集成文章,很多人问我有没有demo, 今天我再次总结一下, 并加个些功能。
- 集成步骤
- 视频预览播放
- 视频放大缩小
- 视频的质量切换
- 截图
- 视频加载细节
之前的文章大家可以看下面的链接:
https://mp.weixin.qq.com/s/v4f2_bky8qfn167ky9gsdg
先看效果图:
![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190906095754634-787402893.gif)
### 集成步骤
###### 1.安装sdk
```
dependencies {
compile 'com.hikvision.ezviz:ezviz-sdk:4.8.6.2'
}
```
###### 2.配置工程
(1).权限配置
在 androidmainfest.xml 文件中添加:
```
//网络权限
//存储权限
//wifi 状态权限
//热点配网扫描wifi需要使用
```
(2).配置build.gradle
```
defaultconfig {
...
ndk {
abifilters "armeabi-v7a"//只支持v7a
}
}
sourcesets {
main {
jnilibs.srcdirs = ['libs']
}
}
```
(3).配置 androidmanifest.xml
添加如下activity定义,用于sdk中间页显示,包含登录、开通云存储等。
```
```
具体可以参考官方集成网站
https://open.ys7.com/doc/zh/book/4.x/android-sdk.html
### 视频预览播放
播放与之前版本不同,**加入设备的验证码**
```
mezplayer.setplayverifycode("验证码");
```
(1).sdk初始化(在application中初始化)
```
/** * sdk日志开关,正式发布需要去掉 */
ezopensdk.showsdklog(true);
/** * 设置是否支持p2p取流,详见api */
ezopensdk.enablep2p(false);
/** * app_key请替换成自己申请的 */
ezopensdk.initlib(this, app_key);
```
(2).播放主要代码
设置token
```
ezopensdk.getinstance().setaccesstoken("你的token");
```
**实现代码:**
布局主要控件是surfaceview,surfaceview的用法大家应该都知道吧,主要实现了implements surfaceholder.callback接口,声明surfaceview 对象,并实方法,代码如下:
```
private surfaceview mrealplaysv = null;
private surfaceholder mrealplaysh = null;
```
```
mrealplaysh = mrealplaysv.getholder();
mrealplaysh.addcallback(videoactivity.this);
```
```
override
public void surfacecreated(surfaceholder holder) {
if (mezplayer != null) {
mezplayer.setsurfacehold(holder);
} else {
}
mrealplaysh = holder;
}
@override
public void surfacechanged(surfaceholder holder, int format, int width, int height) {
}
@override
public void surfacedestroyed(surfaceholder holder) {
if (mezplayer != null) {
mezplayer.setsurfacehold(null);
}
mrealplaysh = null;
}
```
```
```
实现handler.callback,来监听播放结果回调:
```
@override
public boolean handlemessage(message msg) {
switch (msg.what){
}
return false;
}
```
配置:
```
//用来存储萤石云的播放质量、序列号
mcamerainfo=new ezcamerainfo();
// 2-高清,1-标清,0-流畅
mcamerainfo.setvideolevel(2);
//序列号
mcamerainfo.setdeviceserial("c86398971");
mcamerainfo.setcamerano(1);
```
开始播放:
```
private void startrealplay() {
mezplayer = yourapplication.getopensdk().createplayer(mcamerainfo.getdeviceserial(), mcamerainfo.getcamerano());
mezplayer.setplayverifycode(mverificationcode);
mezplayer.sethandler(mhandler);
mezplayer.setsurfacehold(mrealplaysh);
mezplayer.startrealplay();
tvplay.setenabled(true);
}
```
### **视频放大,手势放大功能**
在播放成功的回调里加上 setrealplaysvlayout();方法,方法的代码如下:
```
private void setrealplaysvlayout() throws innerexception, playsdkexception {
displaymetrics dm = new displaymetrics();
getwindowmanager().getdefaultdisplay().getmetrics(dm);
int whdth = dm.widthpixels;
int height = dm.heightpixels;
mrealplaytouchlistener.setsacalerect(constant.max_scale, 0, 0, whdth, height);
setplayscaleui(1, null, null);
}
```
```
private void setplayscaleui(float scale, customrect orect, customrect currect) {
if (scale == 1) {
try {
if (mezplayer != null) {
mezplayer.setdisplayregion(false, null, null);
}
} catch (baseexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
} else {
if (mplayscale == scale) {
try {
if (mezplayer != null) {
mezplayer.setdisplayregion(true, orect, currect);
}
} catch (baseexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return;
}
try {
if (mezplayer != null) {
mezplayer.setdisplayregion(true, orect, currect);
}
} catch (baseexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
mplayscale = scale;
}
```
### 视频质量切换
效果图:
![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190906095808850-1209709960.gif)
核心方法
```
// 2-高清,1-标清,0-流畅
mcamerainfo.setvideolevel
```
切换画质
```
/**
* 码流配置 清晰度 2-高清,1-标清,0-流畅
*
* @see
* @since v2.0
*/
private void setqualitymode(final ezconstants.ezvideolevel mode) {
if (mezplayer != null) {
mwaitdialog.setwaittext("正在设置画面质量…");
mwaitdialog.show();
thread thr = new thread(new runnable() {
@override
public void run() {
try {
// need to modify by yudan at 08-11
app.getopensdk().setvideolevel(mcamerainfo.getdeviceserial(), mcamerainfo.getcamerano(), mode.getvideolevel());
mcurrentqulitymode = mode;
message msg = message.obtain();
msg.what = msg_set_vediomode_success;
mhandler.sendmessage(msg);
} catch (baseexception e) {
mcurrentqulitymode = ezconstants.ezvideolevel.video_level_flunet;
e.printstacktrace();
message msg = message.obtain();
msg.what = msg_set_vediomode_fail;
mhandler.sendmessage(msg);
}
}
}) {
};
thr.start();
}
}
```
切换画质结果:
```
//切换画质成功
case ezconstants.ezrealplayconstants.msg_set_vediomode_success:
handlesetvediomodesuccess();
break;
//切换画质失败
case ezconstants.ezrealplayconstants.msg_set_vediomode_fail:
handlesetvediomodefail(msg.arg1);
break;
```
### 声音与截图
1.声音
核心方法:
```
mlocalinfo.setsoundopen(false);
```
2.截图
核心方法:
```
mezplayer.capturepicture()
```
需要**源码**的朋友可以加我的微信:lengquele5311
![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190906095809708-1381148883.jpg)
大家可以关注我的微信公众号:「秦子帅」一个有质量、有态度的公众号!
![公众号](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190906095809875-938818605.jpg)