Below is the structure of my working code to record video and audio:
Questions:
1) Why is the CamcorderProfile
needed? setProfile(...)
appears to set the dimensions to whatever QUALITY_HIGH gives, but later I set the dimensions I want with setVideoSize(...)
, which overrides this. However, when I remove the two CamcorderProfile lines, the app crashes at setVideoSize(...)
with LogCat E/MediaRecorder(19526): setVideoSize called in an invalid state: 2
.
2) How do I not record audio? The documentation states that if setAudioSource(...)
is not called, there will be no audio track. However, when I remove that line the app crashes at setProfile(...)
with LogCat E/MediaRecorder(19946): try to set the audio encoder without setting the audio source first
.
3) If I remove both CamcorderProfile lines and the setAudioSource(...)
line, it crashes as in 1).
4) I have also tried adding the line
recorder.setOutputFormat(OutputFormat.DEFAULT);
instead of the CamcorderProfile lines. But now it crashes at perpare()
. If setAudioSource(...)
is called the LogCat is: E/MediaRecorder(20737): audio source is set, but audio encoder is not set
if it isn't called the LogCat is: E/MediaRecorder(20544): video source is set, but video encoder is not set
I have looked allover the internet and I can't find a good example of the correct way to setup the MediaRecorder. Here it implies after API 8 you should use the CamcorderProfile class, but it seems to me that it is causing problems.
Any help would be great! Thanks!
Code (which works when run as it is below):
recorder = new MediaRecorder();
recorder.setCamera(<<camera>>);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(profile);
recorder.setOutputFile(<<Path String>>);
recorder.setVideoSize(<<Width>>, <<Height>>);
recorder.setPreviewDisplay(<<Surface>>);
recorder.setOrientationHint(0);
recorder.setMaxDuration(10000);
recorder.setOnInfoListener(this);
try
{
recorder.prepare();
recorder.start();
} catch ...