准备失败:-1在android中的媒体记录器

时间:2012-01-10 12:13:25

标签: android

当我尝试从相机录制视频时出现错误准备失败:-1。

我搜索很多,我无法从任何地方获得任何可接受的答案。这是我的代码。

我不知道参数?并解锁相机。任何人都能感激不尽。

package com.camara.activity;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CamcorderView extends SurfaceView implements
        SurfaceHolder.Callback {

    MediaRecorder recorder;
    SurfaceHolder holder;
    String outputFile = "/sdcard/default.mp4";

    public CamcorderView(Context context, AttributeSet attrs) {
        super(context, attrs);

        holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        recorder.setVideoSize(480, 320);
        recorder.setVideoFrameRate(10);
        recorder.setMaxDuration(10000);

    }

    public void surfaceCreated(SurfaceHolder holder) {

        recorder.setOutputFile(outputFile);
        recorder.setPreviewDisplay(holder.getSurface());
        if (recorder != null) {
            try {
                recorder.prepare();

            } catch (IllegalStateException e) {
                Log.e("IllegalStateException", e.toString());
            } catch (IOException e) {
                Log.e("IOException", e.toString());
            }
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    public void setOutputFile(String filename) {
        outputFile = filename;
        recorder.setOutputFile(filename);
    }

    public void startRecording() {
        recorder.start();
    }

    public void stopRecording() {
        recorder.stop();
        recorder.release();



    }

}

以下是logcat的输出

01-10 17:33:00.450: I/MediaRecorderJNI(14046): prepare: surface=0x243398 (id=1)
01-10 17:33:00.500: E/MediaRecorder(14046): prepare failed: -1
01-10 17:33:00.500: E/IOException(14046): java.io.IOException: prepare failed.

提前感谢。

1 个答案:

答案 0 :(得分:1)

好的,我确实发现了我的错误。因为当然有一个错误。实际上我想同时进行预览和录制,并认为我必须使用相机对象。但实际上,MediaRecorder使用SetDisplayPreview和.... MediaRecord.Prepare自行完成:)

我的心情很好,所以这里的所有课程都是如此;)

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;

public class ModuleVideo implements SurfaceHolder.Callback, OnInfoListener 
{   
    public static final int STATE_NONE      = -1;
    public static final int STATE_STOPPED   = 0;
    public static final int STATE_PREVIEW   = 1;
    public static final int STATE_CAPTURE   = 2;
    public static final int STATE_RECORDING = 3;

    private SurfaceHolder   mHolder;
    private MediaRecorder   mRecorder;
    private SurfaceView     mCameraView;
    private Context         mContext;
    private FrameLayout     mParent;
    private int             mState;
    private boolean         mRecording;


    public ModuleVideo(Context context, FrameLayout parent)
    {
        //Initiate the Surface Holder properly

        mParent     = parent;
        mContext    = context; 
        mRecorder   = null;
        mState      = STATE_NONE;
        mRecording  = false;
    }


    private void Init()
    {
        mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        mRecorder.setProfile(camcorderProfile_HQ);
        mRecorder.setOutputFile("/sdcard/MY_VIDEO.mp4");
    }

    private void Prepare()
    {
        try
        {
            mRecorder.setPreviewDisplay(mHolder.getSurface());
            mRecorder.prepare();
        }
        catch (IllegalStateException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }


    public void UI_StartPreview()
    {
        if(mState == STATE_STOPPED || mState == STATE_NONE)
        {
            mRecorder= new MediaRecorder();

            Init();

            mCameraView= new SurfaceView(mContext);
            mParent.addView(mCameraView);

            this.mHolder = mCameraView.getHolder();
            this.mHolder.addCallback(this);
            this.mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    }


    public void UI_StopPreview()
    {
        if(mState == STATE_PREVIEW)
        {
            if(mRecording)
            {
                UI_StopRecord();
            }

            if(mRecorder != null)
            {
                mRecorder.release();
                mRecorder= null;
            }

            mParent.removeView(mCameraView);
            //mCameraView= null;
        }
    }

    public boolean UI_StartRecord()
    {
        if(mState != STATE_PREVIEW )
        {
            return false;
        }

        String path= "/sdcard/PLUS_VIDEO.mp4";

        String state = android.os.Environment.getExternalStorageState();

        if(!state.equals(Environment.MEDIA_MOUNTED))
        {
                return false;
        }
        File directory = new File(path).getParentFile();
        if(!directory.exists() && !directory.mkdirs())
        {
                return false;
        }

        mRecorder.start();
        mRecording= true;

        mState= STATE_RECORDING;

        return true;
    }


    public void UI_StopRecord()
    {
        if(mRecorder != null)
        {
            mRecorder.stop();
            mRecorder.reset();

            Init();
            Prepare();

            mRecording= false;

            mState= STATE_PREVIEW;
        }
    }

    public boolean UI_IsRecording()
    {
        return mRecording;
    }



    @Override 
    public void onInfo(MediaRecorder mr, int what, int extra)
    { 
        // TODO Auto-generated method stub 
         Log.i(null, "onInfo"); 
    } 


    @Override
    public void surfaceCreated(SurfaceHolder holder) 
    {   
        Prepare();
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
    {
        mState= STATE_PREVIEW;
    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) 
    {

        mState= STATE_STOPPED;
    }
}