我见过这样的问题,但我没有找到解决方案,所以这就是我问这个问题的原因。
当我在阿尔卡特手机上运行我的应用程序并开始视频录制时,一切似乎都很好,直到我按下停止按钮,然后我得到RuntimeException
。我读到我的视频或音频状态不佳,但我无法弄清楚原因。
这是我的代码:
private boolean prepareMediaRecorder(String name) {
if (isCameraOpened()) {
stop();
start();
}
mediaRecorder = new MediaRecorder();
//int orientation = mCameraInfo.orientation;
//int orientation = calcCameraRotation(mDisplayOrientation);
int degrees = 0;
switch (currentOrientation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
/*if (mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (360 - (mCameraInfo.orientation + degrees) % 360) % 360;
} else { // back-facing
result = (mCameraInfo.orientation - degrees + 360) % 360;
}*/
if(mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
result = (mCameraInfo.orientation - degrees + 360) % 360;
// mediaRecorder.setOrientationHint(result);
mediaRecorder.setOrientationHint(result);
} else{
result = (mCameraInfo.orientation + degrees) % 360;
mediaRecorder.setOrientationHint(result);
}
Size pictureSize = null;
SortedSet<Size> sizes = mPreviewSizes.sizes(mAspectRatio);
if (sizes == null) { // Not supported
mAspectRatio = chooseAspectRatio();
sizes = mPreviewSizes.sizes(mAspectRatio);
}
mPictureSizes.clear();
for (Camera.Size size : mCameraParameters.getSupportedVideoSizes()) {
mPictureSizes.add(new Size(size.width, size.height));
}
Size size = chooseOptimalSize(sizes);
final Camera.Size currentSize = mCameraParameters.getPictureSize();
if (currentSize.width != size.getWidth() || currentSize.height != size.getHeight()) {
// Largest picture size in this ratio
pictureSize = mPictureSizes.sizes(mAspectRatio).last();
if (mShowingPreview) {
mCamera.stopPreview();
}
if(mCameraParameters.getSupportedPreviewSizes() != null) {
mCameraParameters.setPreviewSize(size.getWidth(), size.getHeight());
}
setAutoFocusInternal(mAutoFocus);
if(mFlash == 1){
mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(mCameraParameters);
if (mShowingPreview) {
mCamera.startPreview();
}
}
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
//Set source
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(cpHigh);
if(pictureSize != null) {
mediaRecorder.setVideoSize(pictureSize.getWidth(), pictureSize.getHeight());
}
mediaRecorder.setOutputFile(name);
try {
mediaRecorder.prepare();
///mediaRecorder.start();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
然后我在AsyncTask中启动MediaRecorder,这是我的停止方法。
@Override
void stopVideo() {
if(mFlash == 1){
mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
}
if(isVideoRecording == true) {
try {
mediaRecorder.stop();
isVideoRecording = false;
releaseMediaRecorder();
getBackgroundHandler().post(new Runnable() {
@Override
public void run() {
File file = getFile(name);
if (file != null) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.TITLE, file.getName());
values.put(MediaStore.Video.Media.DESCRIPTION, "Taken by All Ears Selfie app");
values.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.SIZE, file.length());
values.put("_data", file.getAbsolutePath());
ContentResolver cr = context.getContentResolver();
cr.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
}
});
} catch (RuntimeException e){
Log.d("TAG", e.getMessage());
File file = getFile(name);
file.delete();
}
}
if(isCameraOpened()){
stop();
start();
}
}
错误:
11-15 19:37:09.063 15282-15282 / com.allearsapp.allearsselfie E / MediaRecorder:停止失败:-1007 11-15 19:37:09.064 15282-15282 / com.allearsapp.allearsselfie D / AndroidRuntime:关闭VM 11-15 19:37:09.089 15282-15282 / com.allearsapp.allearsselfie E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.allearsapp.allearsselfie,PID:15282 java.lang.RuntimeException:停止失败。 在android.media.MediaRecorder.stop(Native方法) 在com.google.android.cameraview.Camera1.stopVideo(Camera1.java:314) 在com.google.android.cameraview.CameraView.stopVideo(CameraView.java:64) 在com.allearsapp.allearsselfie.camera.CameraActivity.stopVideoRecording(CameraActivity.java:410) 在com.allearsapp.allearsselfie.camera.CameraActivity.access $ 300(CameraActivity.java:54) 在com.allearsapp.allearsselfie.camera.CameraActivity $ 2.onClick(CameraActivity.java:288) 在android.view.View.performClick(View.java:4757) 在android.view.View $ PerformClick.run(View.java:19757) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5258) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
编辑:我为Media Recorder添加了OnErrorListener
,我收到此错误:268435556。