1

我一直在尝试制作一个没有音频的录制视频的应用程序(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。我能找到的关于这个错误代码的唯一其他信息是在这篇文章中,但它没有给出一个非常明确的答案,我使用的模型也不是这篇文章中提到的任何一个。

任何人都可以帮助确定错误是什么并提出解决方案吗?

4

2 回答 2

0

您是否尝试过设置 MediaRecorder 配置文件?

    recorder.setProfile(CamcorderProfile.get(camId, CamcorderProfile.QUALITY_LOW));

sof 中有一个类似的问题:

MediaRecorder“启动失败-12”

于 2012-11-28T05:52:55.723 回答
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 时准备好,并在单击按钮时启动。也许您也应该尝试将它们分开并且不要使用相机。请以这种方式尝试,如果可行,请给我反馈。

于 2012-12-01T11:39:52.157 回答