轉https://blog.csdn.net/a287574014/article/details/79745925java
今天在優化相機這塊的時候,看了下系統相機的源碼,copy了部分用MediaRecorder來進行錄製的代碼出來,調用start的時候一直拋異常:ide
- MediaRecorder: start failed: -22
想了很久也不知道什麼,浪費了很長時間以後去百度了一下MediaRecorder詳細信息和用法,最後才發現問題出來了初始化MediaRecorder的時候的調用順序上,MediaRecorder的API是有順序的,不按順序來就會拋異常,坑爹,特此記錄一下。通常的API使用順序爲優化
- try {
- mCamera.unlock();
- mMediaRecorder = new MediaRecorder();
- mMediaRecorder.setCamera(mCamera);
- mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
- mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
- mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
- mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
- mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
- mMediaRecorder.setVideoSize(1920, 1080);
- mMediaRecorder.setVideoFrameRate(30);
- mMediaRecorder.setVideoEncodingBitRate(1024*1024*20);
- mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
- mMediaRecorder.setOutputFile( "/sdcard/lllllll.mp4");
- mMediaRecorder.prepare();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
以前還遇到過MediaRecorder: start failed: -19的 這個錯誤是由於咱們沒在實例化MediaRecorder以前解鎖相機致使編碼