我正在使用视频录制应用程序,我想在其中显示预览,并且当用户单击“录制”按钮时它开始录制,而当用户单击“停止”按钮时它停止录制。
我得到了相机预览,并且记录回相机工作正常。
但是,当我将摄像机翻转到前置摄像机并开始记录时,会出现如下错误:
FATAL EXCEPTION: main java.lang.RuntimeException: start failed. at android.media.MediaRecorder.start(Native Method) at com.opkix.app.fragments.CameraFragment.startRecording( CameraFragment.java:104)
这是我录制视频代码的代码:
private boolean prepareMediaRecorder() { // set the orientation here to enable portrait recording. mediaRecorder = new MediaRecorder(); mCamera.unlock(); mediaRecorder.setCamera(mCamera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); mediaRecorder.setOutputFile(StorageUtils.getOutputMediaFilePath()); mediaRecorder.setMaxDuration(120000); // Set max duration 60 sec. mediaRecorder.setMaxFileSize(100000000); // Set max file size 50Mb mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); try { mediaRecorder.prepare(); } catch (IllegalStateException e) { releaseMediaRecorder(); return false; } catch (IOException e) { releaseMediaRecorder(); return false; } return true; }
有人可以建议解决方案吗?
现在,我也在使用Video Recording App。请一次在其他设备上运行您的代码。我也与您共享我的代码。其中一些代码与我的应用程序相关。如果不需要,请删除那。
我的代码:
private boolean prepareVideoRecorder() { mRecorder = new MediaRecorder(); // Both are required for Portrait Video mCamera.setDisplayOrientation(90); if (mCameraId == CAMERA_FACING_FRONT) { mRecorder.setOrientationHint(270); } else { mRecorder.setOrientationHint(90); } // Step 1: Unlock and set camera to MediaRecorder mCamera.unlock(); mRecorder.setCamera(mCamera); // Step 2: Set sources mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) mRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P)); // Step 4: Set output file final File folder; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { folder = new File(Environment.getExternalStorageDirectory() + "/CameraApp/Videos"); } else { folder = new File(Environment.getExternalStorageDirectory() + "/CameraApp/Videos"); } boolean success = true; File videoFile; if (!folder.exists()) { success = folder.mkdirs(); } if (success) { videoFile = new File(folder.getAbsolutePath() + File.separator + getFileNameCustomFormat() + " " + ".mp4"); SavedVideoPath = getFileNameCustomFormat() + " " + ".mp4"; Log.e("Video Path - ", SavedVideoPath); } else { Toast.makeText(getBaseContext(), "Video Not saved", Toast.LENGTH_SHORT).show(); return true; } mRecorder.setOutputFile(String.valueOf(videoFile)); // mRecorder.setVideoSize(mPreviewWidth, mPreviewHeight); // Step 5: Set the preview output mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); // Step 6: Prepare configured MediaRecorder try { mRecorder.prepare(); } catch (IllegalStateException e) { Toast.makeText(getApplicationContext(), "prepareVideoRecorder() Exception: " + e.getMessage(), Toast.LENGTH_LONG).show(); releaseMediaRecorder(); return false; } catch (IOException e) { Toast.makeText(getApplicationContext(), "prepareVideoRecorder() Exception: " + e.getMessage(), Toast.LENGTH_LONG).show(); releaseMediaRecorder(); return false; } return true; }
让我知道会发生什么。?在尝试我的代码后。希望这会有所帮助: