2012-01-10 50 views
1

當我嘗試從相機記錄視頻,似乎與錯誤準備失敗:-1。準備失敗:-1在Android的媒體記錄器

我搜索很多,我無法從任何地方得到任何可接受的答案。這裏是我的代碼如下。

我不知道它的參數?並解鎖相機。任何人都會很感激。

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

好吧我真的發現我的錯誤。因爲當然有一個錯誤。其實我想同時進行預覽和錄製,並且認爲我必須使用Camera作爲對象。但實際上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; 
    } 
}