接收播放直播视频 (Receive and play live video)
1.动态申请权限 (Dynamic application permission)
public void requestPerm() {
AndPermission.with(this)
.permission(
Permission.Group.MICROPHONE,
Permission.Group.STORAGE,
Permission.Group.CAMERA,
Permission.Group.LOCATION
).onGranted(permissions -> {
}).onDenied(permissions -> Toast.makeText(MainActivity.this, "拒绝麦克风和存储权限无法录像和录音,截屏 Reject microphone and storage permissions, unable to record and record, screen capture", Toast.LENGTH_SHORT).show())
.start();
}
2.初始化解码器,视图View采用 TextureView (Initialize the decoder, view View uses TextureView)
videoDecodec = new glVideoDecodec();
mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface surface1 = new Surface(surface);
videoDecodec.videoDecodecStart(surface1);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Surface surface1 = new Surface(surface);
videoDecodec.videoDecodecStart(surface1);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (videoDecodec != null) {
videoDecodec.videoDecodecStop();
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
3.传入设备UID连接设备并初始化Camera对象(Camera是全局重点需要维护的对象)(The device UID is connected to the device and initializes the Camera object (Camera is the global key object to be maintained))。
private void connectCamera() {
mCamera = new Camera("KHJA-053802-CXCLM");
mCamera.connect("admin", "888888", new Camera.onOffLineCallback() {
@Override
public void Online(Camera m, final int isSuccess) {
Log.e("shurun", "camer ison: " + isSuccess);
if (isSuccess == 0) {
mHandler.sendEmptyMessage(1);
} else {
mHandler.post(() -> Toast.makeText(MainActivity.this, "连接失败: (Connection failed:)" + isSuccess, Toast.LENGTH_LONG).show());
}
}
@Override
public void Offline(Camera m) {
Log.d(MainActivity.TAG, "camera offline");
}
});
}
mCamera.changePassword("888888", newPwd, b -> {
if (b) {
KLog.e("pwd", "原始密码连接成功,修改密码 (The original password is successfully connected, and the password is changed) = " + newPwd);
}
});
4.接收视频,这一步如果一切正常将播放视频 (Receive video, this step will play the video if everything is ok)。
private void startReceiceVideo() {
if (mCamera != null) {
int ret = mCamera.startRecvVideo(new Camera.recvVideoCallback() {
@Override
public void recvVideo(byte[] data, long pts, int keyframe) {
if (!isRunning.get() && keyframe == 1 && videoDecodec != null) {
videoDecodec.videoDecodec(data, pts);
isRunning.set(true);
} else if (isRunning.get() && videoDecodec != null)
videoDecodec.videoDecodec(data, pts);
}
});
Log.e(MainActivity.TAG, "start recv video ret: " + ret);
}
}
5.视频清晰度切换 (Video sharpness switching)
mCamera.setQuality(int VIDEO_QUALITY, b -> {
if(b){
Log.i("khj","Change Video Dpi Success")
}
});
6.停止接受视频 (Stop accepting videos)
mCamera.stopRecvVideo();
7.断开Camera连接 (Disconnect the Camera connection)
mCamera.disconnect(); //该方法只是断开了连接,mCamera还可以使用connect方法重新连接 disconnect //camera but not release,can reconnect
mCamera.release();//这个方法将销毁对象,不能再使用 release camera object