问题描述
我正在使用 MediaRecorder 录制视频,它在大多数设备上都可以正常工作。但在小米 A2、三星 J2、J7 等少数设备上,无论操作系统版本如何,输出录音文件大小均为 0KB。我在清单中打开了 requestLegacyExternalStorage 模式。此外,我尝试创建和保存文件以检查文件生成是否存在任何问题,但没有。我已经添加了下面的大部分代码。
public File getoutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),"Liveliness");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("Liveliness","Failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File videoFile;
videoFile = new File(mediaStorageDir.getPath() + File.separator +
"REC_" + timeStamp + ".mp4");
return videoFile;
}
相机准备代码。
mCamera = cameraSource.getCameraInstance();
mediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a format
mediaRecorder.setoutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setVideoSize(720,480);
mediaRecorder.setorientationHint(270);
CamcorderProfile cpHigh = CamcorderProfile.get(cameraSource.getRequestedCameraId(),CamcorderProfile.QUALITY_480P);
// Set the video encoding bit rate this changes for the high,low.
// medium quality devices
mediaRecorder.setVideoEncodingBitRate(cpHigh.videoBitRate);
//Set the video frame rate
mediaRecorder.setVideoFrameRate(cpHigh.videoFrameRate);
// Step 4: Set output file
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mediaRecorder.setoutputFile(getoutputMediaFile());
}else{
mediaRecorder.setoutputFile(getoutputMediaFile().getPath());
}
// Step 5: Set the preview output
mediaRecorder.setPreviewdisplay(surfaceView.getHolder().getSurface());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mediaRecorder.setInputSurface(MediaCodec.createPersistentInputSurface());
}
mediaRecorder.prepare();
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)