1

我正在使用 Xamarin 的 Mono for Android。当我调用媒体记录器的准备方法时,我收到此错误:

java.io.IOException:准备失败。

这是我的代码:

record.Click += delegate {
            try {



                path = Android.OS.Environment.ExternalStorageDirectory.Name + "/" + etVideoName.Text + ".mp4";
                video.StopPlayback ();

                recorder = new MediaRecorder ();
                recorder.SetVideoSource (VideoSource.Camera); 
                recorder.SetAudioSource (AudioSource.Mic);              
                recorder.SetOutputFormat (OutputFormat.Mpeg4);
                recorder.SetVideoEncoder (VideoEncoder.Default); 
                recorder.SetAudioEncoder (AudioEncoder.Default);      
                recorder.SetOutputFile (path);       
                //recorder.SetPreviewDisplay (video.Holder.Surface);         
                recorder.Prepare ();
                recorder.Start ();   
            } catch (Exception ex) {
                MakeAlert("Error", ex.ToString());
            }
        };
4

1 回答 1

0

这是一个记录器 100% 工作学习它:)

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



    }

   }
于 2013-06-30T23:56:27.503 回答