用前置摄像头录制视频

时间:2012-12-15 12:10:01

标签: android

我在前置模式下打开相机时出错并开始录制。 该应用程序崩溃。

我的代码是:

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements SurfaceHolder.Callback{

     Camera camera;
     SurfaceView surfaceView;
     SurfaceHolder surfaceHolder;
     boolean previewing = false;
     MediaRecorder m_recorder;
     int MAX_TIME=5;
 String m_path;
 String stringPath = "/sdcard/samplevideo.3gp";
 MediaRecorder videoRecorder;
 Surface surface;
 MediaRecorder mrec;
     /** Called when the activity is first created.**/
       @Override
       public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
   Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);

   getWindow().setFormat(PixelFormat.UNKNOWN);
   surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
   surfaceHolder = surfaceView.getHolder();
   surfaceHolder.addCallback(this);
   surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v)
   {
     if(!previewing){
     Camera.CameraInfo cameraInfo = new Camera.CameraInfo();

     for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
         CameraInfo camInfo = new CameraInfo();
         Camera.getCameraInfo(camNo, camInfo);

         //Toast.makeText(MainActivity.this, camNo, Toast.LENGTH_LONG).show();
         if (camInfo.facing==(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
             camera = Camera.open(1);
         }
      }
 try{          
     camera.setDisplayOrientation(90);
     camera.setPreviewDisplay(surfaceHolder);
    }catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
 if (camera != null){
   try
   {
   camera.setPreviewDisplay(surfaceHolder);
   camera.startPreview();
   previewing = true;

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

 System.out.println("before MediaRecorder");
      mrec = new MediaRecorder(); // Works well
     System.out.println("after MediaRecorder");
     camera.unlock();
     System.out.println("after Unlock");   
     mrec.setCamera(camera);
     System.out.println("setcamera"); 
     mrec.setPreviewDisplay(surfaceHolder.getSurface());
     System.out.println("setcamera1"); 
     mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
     System.out.println("setcamera2"); 
     mrec.setAudioSource(MediaRecorder.AudioSource.MIC); 

     mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
     mrec.setPreviewDisplay(surfaceHolder.getSurface());

     mrec.setOutputFile("/sdcard/recordvideooutput.3gpp"); 

     try {
         System.out.println("before prepare"); 
        mrec.prepare();

        System.out.println("after prepare");
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     //
}
   }});

 buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
   try {
       camera.reconnect();
       camera.unlock();
} catch (Exception e) {
    // TODO: handle exception
}

   mrec.start();
// TODO Auto-generated method stub
/*if(camera != null && previewing){
 camera.stopPreview();
 camera.release();
 camera = null;

 previewing = false;
}*/
   }});

   }



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

 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {

 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {


 }
}

请告诉我错误的地方。 如果您使用前置摄像头为我提供完整的视频录制代码,那么我会对我有所帮助。

谢谢先进。

0 个答案:

没有答案