Android视频摄像头预览

时间:2012-06-12 12:37:32

标签: android video-capture preview mediarecorder

我想在录制视频之前进行预览。我可以拍得很好但我无法在它之前得到预览。在这个时刻,我确实有预览但是如果我尝试开始捕获或者删除预览并且可以正常捕获我会收到错误。

布局是一个surfaceView和一个小图像视图(红点),让你知道你正在录制

public class MediaVideoCameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener{

         private ImageView reddot;
         private Camera camera;
         private SurfaceView camcorderView;
         private SurfaceHolder camcorderHolder;
         private MediaRecorder recorder;
         private String PATH = "/sdcard";
         private boolean recording = false;
         private static int id = 1;
         private boolean previewRunning = false;

         /** Called when the activity is first created. */ 
         @Override 
         public void onCreate(Bundle savedInstanceState) { 
              super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.mediavideocamera);

            id++;

            reddot = (ImageView) findViewById(R.id.reddot);
            reddot.setAlpha(0);

            camera = Camera.open();

            camcorderView = (SurfaceView) findViewById(R.id.surfaceVideoCamera);
            camcorderView.setOnClickListener(this);
            camcorderHolder = camcorderView.getHolder();
            camcorderHolder.addCallback(this);
            camcorderHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            recorder = new MediaRecorder();
            recorder.setCamera(camera);
            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);

            File sdImageMainDirectory = new File(PATH);
            recorder.setOutputFile(sdImageMainDirectory + "/Video" + id + ".mp4");
            Log.d("VideoCamera", "Made File: " + sdImageMainDirectory + "/Video" + id + ".mp4");

         } 

         public void onClick(View arg0) {
              if (recording) { 

                  recorder.stop();  
                  recorder.release();
                  //safe it here or new activity

                  finish(); 
              } else {

              recording = true;   
              camera.stopPreview();
               recorder.setPreviewDisplay(camcorderHolder.getSurface());

              try {
                    recorder.prepare();

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


              recorder.start();

                reddot.setAlpha(100);

              } 
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w,
                int h) {
            // TODO Auto-generated method stub
            if(recording == false){
                if (previewRunning) {
                    camera.stopPreview();
                } else {

                Camera.Parameters p = camera.getParameters();
                p.setPreviewSize(w, h);
                camera.setParameters(p);
                try {
                    camera.setPreviewDisplay(holder);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                camera.startPreview();
                previewRunning = true;
                }
            }
        }

        public void surfaceCreated(SurfaceHolder arg0) {

        }

        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub

        }        
}</code>

The error is

public class MediaVideoCameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener{

         private ImageView reddot;
         private Camera camera;
         private SurfaceView camcorderView;
         private SurfaceHolder camcorderHolder;
         private MediaRecorder recorder;
         private String PATH = "/sdcard";
         private boolean recording = false;
         private static int id = 1;
         private boolean previewRunning = false;

         /** Called when the activity is first created. */ 
         @Override 
         public void onCreate(Bundle savedInstanceState) { 
              super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.mediavideocamera);

            id++;

            reddot = (ImageView) findViewById(R.id.reddot);
            reddot.setAlpha(0);

            camera = Camera.open();

            camcorderView = (SurfaceView) findViewById(R.id.surfaceVideoCamera);
            camcorderView.setOnClickListener(this);
            camcorderHolder = camcorderView.getHolder();
            camcorderHolder.addCallback(this);
            camcorderHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            recorder = new MediaRecorder();
            recorder.setCamera(camera);
            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);

            File sdImageMainDirectory = new File(PATH);
            recorder.setOutputFile(sdImageMainDirectory + "/Video" + id + ".mp4");
            Log.d("VideoCamera", "Made File: " + sdImageMainDirectory + "/Video" + id + ".mp4");

         } 

         public void onClick(View arg0) {
              if (recording) { 

                  recorder.stop();  
                  recorder.release();
                  //safe it here or new activity

                  finish(); 
              } else {

              recording = true;   
              camera.stopPreview();
               recorder.setPreviewDisplay(camcorderHolder.getSurface());

              try {
                    recorder.prepare();

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


              recorder.start();

                reddot.setAlpha(100);

              } 
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w,
                int h) {
            // TODO Auto-generated method stub
            if(recording == false){
                if (previewRunning) {
                    camera.stopPreview();
                } else {

                Camera.Parameters p = camera.getParameters();
                p.setPreviewSize(w, h);
                camera.setParameters(p);
                try {
                    camera.setPreviewDisplay(holder);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                camera.startPreview();
                previewRunning = true;
                }
            }
        }

        public void surfaceCreated(SurfaceHolder arg0) {

        }

        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub

        }        
}</code>

0 个答案:

没有答案