Android - MediaRecorder启动失败:RuntimeException

时间:2015-08-28 07:17:41

标签: android android-camera

我正在使用此活动在我的应用程序中录制视频。当我运行此代码时,我收到以下错误:

Video dimension (1600x1200) is unsupported.
E/MediaRecorder﹕ start failed: -19
...
Caused by: java.lang.RuntimeException: start failed.

我在Manifest中指定了以下权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />

这是活动本身:

public class CameraActivity extends Activity {

private Camera mCamera;
private SurfaceView mPreview;
private MediaRecorder mMediaRecorder;
private boolean isRecording = false;
private static final String TAG = "Recorder";
private Button captureButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPreview = (SurfaceView) findViewById(R.id.surface_view);
    captureButton = (Button) findViewById(R.id.button_capture);
}

public void onCaptureClick(View view) {
    if (isRecording) {
        mMediaRecorder.stop(); 
        releaseMediaRecorder(); 
        mCamera.lock();
        setCaptureButtonText("Capture");
        isRecording = false;
        releaseCamera();
    } else {
        new MediaPrepareTask().execute(null, null, null);
    }
}

private void setCaptureButtonText(String title) {
    captureButton.setText(title);
}

@Override
protected void onPause() {
    super.onPause();
    releaseMediaRecorder();
    releaseCamera();
}

private void releaseMediaRecorder() {
    if (mMediaRecorder != null) {
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mMediaRecorder = null;
        mCamera.lock();
    }
}

private void releaseCamera() {
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private boolean prepareVideoRecorder() {
    mCamera = CamHelper.getDefaultCameraInstance();
    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size optimalSize = CamHelper.getOptimalPreviewSize(mSupportedPreviewSizes,
            mPreview.getWidth(), mPreview.getHeight());
    CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_HIGH);
    profile.videoFrameWidth = optimalSize.width;
    profile.videoFrameHeight = optimalSize.height;

    parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
    mCamera.setParameters(parameters);
    try {
        mCamera.setPreviewDisplay(mPreview.getHolder());
    } catch (IOException e) {
        Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
        return false;
    }


    mMediaRecorder = new MediaRecorder();

    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

    mMediaRecorder.setProfile(profile);

    mMediaRecorder.setOutputFile("/sdcard/schickn_sample.mp4");

    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}

class MediaPrepareTask extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Void... voids) {
        if (prepareVideoRecorder()) {
            mMediaRecorder.start();
            isRecording = true;
        } else {
            releaseMediaRecorder();
            return false;
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (!result) {
            CameraActivity.this.finish();
        }
        setCaptureButtonText("Stop");

    }
}

最低Api版本设置为11.我在网上查看了有关同一问题的其他几个问题,但没有一个解决方案对我有用。我试过了:

https://stackoverflow.com/a/20388192/3011902

但它没有解决这个例外。

1 个答案:

答案 0 :(得分:0)

由于错误消息

  

不支持视频尺寸(1600x1200)。

我认为你的参数做错了。尝试将它们保留为默认设置或更改值以解决问题。

    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size optimalSize = CamHelper.getOptimalPreviewSize(mSupportedPreviewSizes,
            mPreview.getWidth(), mPreview.getHeight());
    CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_HIGH);
    profile.videoFrameWidth = optimalSize.width;
    profile.videoFrameHeight = optimalSize.height;