Ivanzar Ivanzar - 21 days ago 7
Android Question

MediaRecorder: audio source is set, but audio encoder is not set

I use

Camera
,
Mediarecorder
,
mMediaRecorder.setCaptureRate(0.1)
, and
CamcorderProfile.QUALITY_TIME_LAPSE_LOW
. I do not know what the problem is.

Code:

private boolean prepareVideoRecorder(){

if(mCamera == null) {
mCamera = getCameraInstance();
}
mMediaRecorder = new MediaRecorder();

// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);

// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_LOW));

// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

// Step 5.5: Set the video capture rate to a low number
mMediaRecorder.setCaptureRate(0.1); // capture a frame every 10 seconds

// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d("ADS", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d("ADS", "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}


Error:

11-12 17:07:52.942 18218-18218/ivanzar.ads E/MediaRecorder: audio source is set, but audio encoder is not set
11-12 17:07:52.942 18218-18218/ivanzar.ads D/ADS: IllegalStateException preparing MediaRecorder: null

Answer

This is my code to record videos:

@Override
                public void surfaceCreated(SurfaceHolder holder) {
                    Camera.Parameters params = mServiceCamera.getParameters();
                    mServiceCamera.setParameters(params);
                    Camera.Parameters p = mServiceCamera.getParameters();

                    List<Camera.Size> listSize;

                    listSize = p.getSupportedPreviewSizes();
                    Camera.Size mPreviewSize = listSize.get(0);
                    p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                    mServiceCamera.setParameters(p);

                    try {
                        mServiceCamera.setPreviewDisplay(holder);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mServiceCamera.startPreview();

                    mServiceCamera.unlock();

                    mMediaRecorder = new MediaRecorder();
                    mMediaRecorder.setCamera(mServiceCamera);
                    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
                    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
                    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    mMediaRecorder.setVideoEncodingBitRate(3000000);
                    mMediaRecorder.setVideoSize(1280, 720);
                    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                    mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getPath() + "/" + STrVideoname);
                    mMediaRecorder.setPreviewDisplay(holder.getSurface());
                    mMediaRecorder.setOrientationHint(90);
                    try {
                        mMediaRecorder.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mMediaRecorder.start();

                    mRecordingStatus = true;


                }
Comments