我以前寫過一篇螢石雲的集成文章,不少人問我有沒有demo, 今天我再次總結一下, 並加個些功能。html
以前的文章你們能夠看下面的連接: mp.weixin.qq.com/s/V4F2_bkY8…android
先看效果圖: api
dependencies {
compile 'com.hikvision.ezviz:ezviz-sdk:4.8.6.2'
}
複製代碼
(1).權限配置 在 AndroidMainfest.xml 文件中添加:bash
//網絡權限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
//存儲權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//wifi 狀態權限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
//熱點配網掃描wifi須要使用
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
複製代碼
(2).配置build.gradle微信
defaultConfig {
...
ndk {
abiFilters "armeabi-v7a"//只支持v7a
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
複製代碼
(3).配置 AndroidManifest.xml 添加以下activity定義,用於sdk中間頁顯示,包含登陸、開通雲存儲等。網絡
<activity
android:name="com.videogo.main.EzvizWebViewActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
複製代碼
具體能夠參考官方集成網站 open.ys7.com/doc/zh/book…app
播放與以前版本不一樣,加入設備的驗證碼ide
mEZPlayer.setPlayVerifyCode("驗證碼");
複製代碼
(1).SDK初始化(在application中初始化)佈局
/** * sdk日誌開關,正式發佈須要去掉 */
EZOpenSDK.showSDKLog(true);
/** * 設置是否支持P2P取流,詳見api */
EZOpenSDK.enableP2P(false);
/** * APP_KEY請替換成本身申請的 */
EZOpenSDK.initLib(this, APP_KEY);
複製代碼
(2).播放主要代碼 設置tokengradle
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;
}
複製代碼
<SurfaceView
android:id="@+id/realplay_sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent" />
複製代碼
實現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;
}
複製代碼
效果圖:
核心方法
// 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
你們能夠關注個人微信公衆號:「秦子帥」一個有質量、有態度的公衆號!