通过Android 2.2版本从Camera录制视频

|
enter code here
当我尝试从Android 2.2版的摄像机录制视频时。它有一些错误,没人能找到解决方案。是否有任何错误的Android MediaRecorder API。我该如何解决。 我有更多错误。您可以在图片中看到其中的一些错误。 像这样的错误:Camera Preview -13 非常感谢。 http://i.stack.imgur.com/72lp7.png records.prepare()失败并抛出Java.lang.illegalexeption 这是代码:
package app.raceway.com;

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

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class VideoCapture extends Activity implements  SurfaceHolder.Callback {
    MediaRecorder recorder;
    SurfaceHolder holder;
    public Camera camera;
    File video;
    String filePath;
    boolean recording = false;
      private static final int FRAME_RATE = 15;

      private static final int CIF_WIDTH = 320;

      private static final int CIF_HEIGHT = 240;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    recorder = new MediaRecorder();

    setContentView(R.layout.main);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.cameraView);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    String path=Environment.getExternalStorageDirectory().getAbsolutePath()+
    \"/video/videocapture_example.mpg4\";
    // make sure the directory we plan to store the recording in exists
    File sampleDir = Environment.getExternalStorageDirectory();
    try { 
        video = new File(sampleDir+\"/videofile.3gp\");
        sampleDir.createNewFile();
          //video = File.createTempFile(\"videofile\", \".3gp\", sampleDir);
      } 
      catch (IOException e)
      {
          Log.e(\"deneme\",\"sdcard access error\");

      }
      filePath=video.getAbsolutePath();

}

private void initRecorder() {

    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

}
private void prepareRecorder() throws IOException{
    recorder.setCamera(camera);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
    recorder.setOutputFile(filePath);
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
try {

} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


}



public void onclickSaveVideo(View v) throws IOException {
    if (recording) {
    Toast t=new Toast(getApplicationContext());
    t.makeText(getApplicationContext(), \"Video Recording stopped\",Toast.LENGTH_SHORT);
    t.show();
        recorder.stop();
        recording = false;

        // Let\'s initRecorder so we can record again
        initRecorder();     
    } else {
        try {
            prepareRecorder();
            //recorder.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        recording = true;
        recorder.prepare();
        recorder.start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        recorder.stop();
        Toast t=new Toast(getApplicationContext());
        t.makeText(getApplicationContext(), \"Video Recording started\",Toast.LENGTH_SHORT);
        t.show();
    }
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera=Camera.open();

    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    camera.startPreview();
    camera.unlock();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    finish();
    camera.release();
}


}
    
已邀请:
        艾敏 根据logcat输出的图像,您提供的崩溃是通过
start()
方法发生的。您会从
start
方法的文档中看到必须先调用
prepare()
,否则它将抛出
IllegalStateException
。在您的代码中,所有
prepare()
的调用都被注释掉了? 编辑:我们在我的答案下方的评论中对事情进行了整理。他在仿真器上运行此代码,仿真器不支持ѭ7。     

要回复问题请先登录注册