android - java.lang.RuntimeException : start failed(Front camera recording)

标签 android video android-camera runtimeexception front-camera

我在尝试从前置摄像头录制视频时遇到异常。但是后置摄像头录制工作正常。崩溃在线 mMediaRecorder.start();

java.lang.RuntimeException: start failed.
       at android.media.MediaRecorder.start(MediaRecorder.java)
       at xyz.CameraFragment$6.onClick(CameraFragment.java:270)
       at android.view.View.performClick(View.java:4466)
       at android.view.View$PerformClick.run(View.java:18537)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5102)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
       at dalvik.system.NativeStart.main(NativeStart.java)

下面是我的代码-

private boolean prepareVideoRecorder() {
    mMediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    mVideoFile = new File(getOutputMediaFile().toString());
    mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath());
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
    mMediaRecorder.setOrientationHint(270);
    mMediaRecorder.setMaxDuration(10000);
    mMediaRecorder.setOnInfoListener(this);
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("CAMERA", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d("CAMERA", "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}
if (prepareVideoRecorder()) {
    // Camera is available and unlocked, MediaRecorder is prepared,
    // now you can start recording
    mMediaRecorder.start();
}

最佳答案

基于spitzanator的回答:

  1. 确保您的权限正确:

<uses-feature android:name="android.hardware.camera.front" />

  • 显然这行代码不适用于前置摄像头:
  • mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    spitzanator 还说:CamcorderProfile.get() 的签名默认为后置摄像头的配置文件:

    Returns the camcorder profile for the first back-facing camera on the device at the given quality level. If the device has no back-facing camera, this returns null.

    理想的解决方案可以找到here .

    旁注:由于声誉较低,我无法发表评论而不是发布答案,因此所有积分均归于 spitzanator .

    关于android - java.lang.RuntimeException : start failed(Front camera recording),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33239715/

    相关文章:

    javascript - 页面上有多个视频需要自动播放单击的按钮之一

    Android 使用 native 相机保存图像

    android - 当相机在 Android 中的表面 View 上实时显示时以编程方式截屏

    java - 缩放 Canvas 后位图质量丢失

    android - 显示 snackbar 时,来自设计支持库的 FAB 不会向上移动

    android - Intent 在 Android 上播放 SD 卡上文件中的视频

    video - YouTube 数据 API (v3) : Getting video source url

    android - java.lang.IllegalArgumentException : Scrapped or attached views may not be recycled. isScrap:false isAttached:true 向下滚动时

    android - 自动阻止应用程序从启动器启动

    android - D/skia : --- Failed to create image decoder with message 'unimplemented'