我以前寫過一篇螢石雲的集成文章,不少人問我有沒有demo, 今天我再次總結一下, 並加個些功能。html
以前的文章你們能夠看下面的連接:
https://mp.weixin.qq.com/s/V4...android
先看效果圖:
api
dependencies { compile 'com.hikvision.ezviz:ezviz-sdk:4.8.6.2' }
(1).權限配置
在 AndroidMainfest.xml 文件中添加:微信
//網絡權限 <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中間頁顯示,包含登陸、開通雲存儲等。app
<activity android:name="com.videogo.main.EzvizWebViewActivity" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"> </activity>
具體能夠參考官方集成網站
https://open.ys7.com/doc/zh/b...ide
播放與以前版本不一樣,加入設備的驗證碼佈局
mEZPlayer.setPlayVerifyCode("驗證碼");
(1).SDK初始化(在application中初始化)gradle
/** * 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; }
<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
你們能夠關注個人微信公衆號:「秦子帥」一個有質量、有態度的公衆號!