Android的MediaRecorder启动失败:-12 [英] Android MediaRecorder start failed: -12

查看:277
本文介绍了Android的MediaRecorder启动失败:-12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图使该记录的视频,不带音频的应用程序(API 8)。我跟着在Android教程中的说明。我的code是如下:

I've been trying to make an app (API 8) that records video, without audio. I've followed the instructions on the Android tutorial. My code is as follows:

    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异常即不能抓住了,有错误code沿启动失败:-12
唯一的其他信息,我可以找到关于此错误的code在这个的职位,但它没有给出一个明确的答案,也不是我使用任何在后处理的那些模型。

When it reaches recorder.start(), the program crashes, throwing an IllegalStateException that can't be caught, along with an error code start failed: -12. The only other info I could find about this error code was in this post, but it doesn't give a very clear answer, nor is the model I'm using any of the ones addressed in the post.

谁能帮助找出错误是什么,并提出一个解决方案?

Can anyone help identify what the error is and suggest a solution?

推荐答案

所以现在从来就尝试过一个例子,下载的来源。我和你的code修改了它和它的作品不会对API 8.这节省的声音是主类:

So now I´ve tried an example and downloaded the sources. I modified it with Your code and it works without saving sound on API 8. Here is the main class:

    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();
    }
}

}

要你的code不同的是,在本教程中没有摄像头被实例化。它仅适用于MediaRecorder。另一个不同之处,即prepare()和start()方法是分开的。该MediaRecorder在启动所述应用程序,$ P $时创建SurfaceView ppared初始化,并且在按钮点击启动。也许你应该尝试它们分开过,并请勿使用相机。请尝试这种方式,并给我一个反馈,如果它的工作原理。

The difference to Your code is, that in this tutorial no camera is instantiated. It only works with MediaRecorder. The other difference is, that the prepare() and start() methods are separated. The MediaRecorder is initialized at starting the app, prepared when SurfaceView is created, and started at button click. Maybe You should try to seperate them too and don´t use camera. Please try it that way and give me a feedback if it works.

这篇关于Android的MediaRecorder启动失败:-12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆