我一直在努力创建一个记录视频的应用程序(API 8),没有音频。我按照Android教程的说明进行操作。我的代码如下:
mCamera.unlock();
recorder = new MediaRecorder();
recorder.setCamera(mCamera);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
//getOutputMediaFile returns a file path where the video will be saved
recorder.setPreviewDisplay(mHolder.getSurface());
try {
recorder.prepare();
recorder.start();
recording = true;
} catch (IllegalStateException e) {
System.out.println("Error preparing recorder");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error preparing video output");
e.printStackTrace();
}
当它达到recorder.start()
时,程序崩溃,抛出无法捕获的IllegalStateException
以及错误代码start failed: -12
。
关于这个错误代码的唯一其他信息是在this帖子中,但它没有给出一个非常明确的答案,模型我也没有使用帖子中提到的任何一个。
任何人都可以帮助确定错误是什么并建议解决方案吗?
答案 0 :(得分:0)
您是否尝试过设置MediaRecorder配置文件?
recorder.setProfile(CamcorderProfile.get(camId, CamcorderProfile.QUALITY_LOW));
在这里有一个类似的问题:
答案 1 :(得分:0)
所以现在我尝试了一个例子并下载了源代码。我用你的代码对它进行了修改,它可以在API 8上保存声音。这是主要类:
public class AndroidVideoCapture extends Activity implements SurfaceHolder.Callback{
Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
mediaRecorder = new MediaRecorder();
initMediaRecorder();
setContentView(R.layout.main);
SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
private Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(recording){
mediaRecorder.stop();
mediaRecorder.release();
finish();
}else{
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}};
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
private void initMediaRecorder(){
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
}
private void prepareMediaRecorder(){
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
与您的代码的区别在于,在本教程中没有实例化相机。它仅适用于MediaRecorder。另一个区别是,prepare()和start()方法是分开的。 MediaRecorder在启动应用程序时初始化,在创建SurfaceView时准备,并在按钮单击时启动。也许你应该尝试将它们分开并且不要使用相机。请尝试这种方式,如果有效,请给我反馈。