Browse Source

修复无法屏蔽声音问题,新增重启Camera接口

main
jiangdongguo 7 years ago
parent
commit
e2921e7135
  1. 17
      app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
  2. 10
      app/src/main/res/layout/activity_usbcamera.xml
  3. 27
      libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java
  4. 2
      libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java
  5. 17
      libusbcamera/src/main/java/com/serenegiant/usb/encoder/biz/Mp4MediaMuxer.java

17
app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java

@ -48,6 +48,8 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
public Button mBtnRecord; public Button mBtnRecord;
@BindView(R.id.btn_update_resolution) @BindView(R.id.btn_update_resolution)
public Button mBtnUpdateResultion; public Button mBtnUpdateResultion;
@BindView(R.id.btn_restart_camera)
Button mBtnRestartCamera;
private USBCameraManager mUSBManager; private USBCameraManager mUSBManager;
private CameraViewInterface mUVCCameraView; private CameraViewInterface mUVCCameraView;
@ -130,10 +132,23 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
} }
} }
@OnClick({R.id.camera_view, R.id.btn_capture_pic, R.id.btn_rec_video,R.id.btn_update_resolution}) @OnClick({R.id.camera_view, R.id.btn_capture_pic, R.id.btn_rec_video,R.id.btn_update_resolution,R.id.btn_restart_camera})
public void onViewClick(View view) { public void onViewClick(View view) {
int vId = view.getId(); int vId = view.getId();
switch (vId) { switch (vId) {
// 重启Camera
case R.id.btn_restart_camera:
if(mUSBManager == null)
return;
mUSBManager.restartUSBCamera(USBCameraActivity.this, mUVCCameraView, new USBCameraManager.OnPreviewListener() {
@Override
public void onPreviewResult(boolean isSuccess) {
if(isSuccess) {
showShortMsg("重启成功");
}
}
});
break;
// 切换分辨率 // 切换分辨率
case R.id.btn_update_resolution: case R.id.btn_update_resolution:
if(mUSBManager == null) if(mUSBManager == null)

10
app/src/main/res/layout/activity_usbcamera.xml

@ -45,4 +45,14 @@
android:textSize="16sp" android:textSize="16sp"
android:text="切换分辨率为320x240"/> android:text="切换分辨率为320x240"/>
<Button
android:layout_above="@id/btn_update_resolution"
android:id="@+id/btn_restart_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="重启摄像头"/>
</RelativeLayout> </RelativeLayout>

27
libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java

@ -93,8 +93,6 @@ public class USBCameraManager{
if(listener != null){ if(listener != null){
listener.onDettachDev(device); listener.onDettachDev(device);
} }
// 释放资源
release();
} }
// 当连接到USB Camera时,被回调 // 当连接到USB Camera时,被回调
@ -162,6 +160,31 @@ public class USBCameraManager{
}); });
} }
public void restartUSBCamera(Activity activity, CameraViewInterface cameraView,final OnPreviewListener mPreviewListener){
// 关闭摄像头
closeCamera();
// 释放CameraHandler占用的相关资源
if(mCameraHandler != null){
mCameraHandler.release();
mCameraHandler = null;
}
// 重新初始化mCameraHandler
cameraView.setAspectRatio(previewWidth / (float)previewHeight);
mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE,
previewWidth,previewHeight,PREVIEW_FORMAT);
openCamera(mCtrlBlock);
// 开始预览
startPreview(cameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() {
@Override
public void onPreviewResult(boolean result) {
if(mPreviewListener != null){
mPreviewListener.onPreviewResult(result);
}
}
});
}
/** /**
* 注册检测USB设备广播接收器 * 注册检测USB设备广播接收器
* */ * */

2
libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java

@ -667,7 +667,7 @@ public abstract class AbstractUVCCameraHandler extends Handler {
// 初始化混合器 // 初始化混合器
videoPath = params.getRecordPath(); videoPath = params.getRecordPath();
mMuxer = new Mp4MediaMuxer(params.getRecordPath(), mMuxer = new Mp4MediaMuxer(params.getRecordPath(),
params.getRecordDuration() * 60 * 1000); params.getRecordDuration() * 60 * 1000,params.isVoiceClose());
// 启动视频编码线程 // 启动视频编码线程
startVideoRecord(); startVideoRecord();

17
libusbcamera/src/main/java/com/serenegiant/usb/encoder/biz/Mp4MediaMuxer.java

@ -27,10 +27,12 @@ public class Mp4MediaMuxer {
private long mBeginMillis; private long mBeginMillis;
private MediaFormat mVideoFormat; private MediaFormat mVideoFormat;
private MediaFormat mAudioFormat; private MediaFormat mAudioFormat;
private boolean isVoiceClose;
// 文件路径;文件时长 // 文件路径;文件时长
public Mp4MediaMuxer(String path, long durationMillis) { public Mp4MediaMuxer(String path, long durationMillis,boolean isVoiceClose) {
String mFilePath; String mFilePath;
this.isVoiceClose = isVoiceClose;
this.durationMillis = durationMillis; this.durationMillis = durationMillis;
if(durationMillis != 0) { if(durationMillis != 0) {
mFilePath = path + "-" + index++ + ".mp4"; mFilePath = path + "-" + index++ + ".mp4";
@ -49,11 +51,9 @@ public class Mp4MediaMuxer {
} }
} }
public synchronized void addTrack(MediaFormat format, boolean isVideo) { public synchronized void addTrack(MediaFormat format, boolean isVideo) {
// now that we have the Magic Goodies, start the muxer // now that we have the Magic Goodies, start the muxer
if (mAudioTrackIndex != -1 && mVideoTrackIndex != -1) if ((!isVoiceClose && mAudioTrackIndex != -1) && mVideoTrackIndex != -1)
throw new RuntimeException("already add all tracks"); throw new RuntimeException("already add all tracks");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
@ -63,7 +63,9 @@ public class Mp4MediaMuxer {
if (isVideo) { if (isVideo) {
mVideoFormat = format; mVideoFormat = format;
mVideoTrackIndex = track; mVideoTrackIndex = track;
if (mAudioTrackIndex != -1) { // 当音频轨道添加
// 或者开启静音就start
if (isVoiceClose || mAudioTrackIndex != -1) {
if (VERBOSE) if (VERBOSE)
Log.i(TAG, "both audio and video added,and muxer is started"); Log.i(TAG, "both audio and video added,and muxer is started");
mMuxer.start(); mMuxer.start();
@ -81,7 +83,7 @@ public class Mp4MediaMuxer {
} }
public synchronized void pumpStream(ByteBuffer outputBuffer, MediaCodec.BufferInfo bufferInfo, boolean isVideo) { public synchronized void pumpStream(ByteBuffer outputBuffer, MediaCodec.BufferInfo bufferInfo, boolean isVideo) {
if (mAudioTrackIndex == -1 || mVideoTrackIndex == -1) { if ((!isVoiceClose && mAudioTrackIndex == -1) || mVideoTrackIndex == -1) {
// Log.i(TAG, String.format("pumpStream [%s] but muxer is not start.ignore..", isVideo ? "video" : "audio")); // Log.i(TAG, String.format("pumpStream [%s] but muxer is not start.ignore..", isVideo ? "video" : "audio"));
return; return;
} }
@ -131,7 +133,8 @@ public class Mp4MediaMuxer {
public synchronized void release() { public synchronized void release() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (mMuxer != null) { if (mMuxer != null) {
if (mAudioTrackIndex != -1 && mVideoTrackIndex != -1) { //(!isVoiceClose&&mAudioTrackIndex != -1)
if (mVideoTrackIndex != -1) {
if (VERBOSE) if (VERBOSE)
Log.i(TAG, String.format("muxer is started. now it will be stoped.")); Log.i(TAG, String.format("muxer is started. now it will be stoped."));
try { try {

Loading…
Cancel
Save