我不断收到“11-15 01:00:53.185: E/MediaRecorder(976): setOutputFormat called in an invalid state: 1” on "recorder.setProfile(profile); "
package com.apress.proandroidmedia.ch1.cameraintent;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class JustRecord extends Activity implements SurfaceHolder.Callback{
SurfaceView cameraView;
SurfaceHolder surfaceHolder;
Camera camera;
MediaRecorder recorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
cameraView = (SurfaceView) findViewById(R.id.CameraView);
surfaceHolder = cameraView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try{
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
camera.setDisplayOrientation(90);
}
catch (Exception exception){
camera.release();
exception.printStackTrace();
}
recorder = new MediaRecorder();
recorder.setCamera(camera);
CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_HIGH);
if(profile != null) {
recorder.setProfile(profile);
}else {
//default to basic H263 and AMR_NB if profile not found
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
}
recorder.setOutputFile("/sdcard/videocapture_example.mp4");
recorder.setMaxDuration(5000); // 50 seconds
prepareRecorder();
recorder.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
//camera.stopPreview();
recorder.stop();
recorder.release();
camera.release();
}
private void prepareRecorder() {
recorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
}
有任何想法吗?谢谢!
编辑:我不妨补充一点,如果我输入编解码器等,代码就可以正常工作。只是由于某种原因我无法确定最好的质量。