android 录像

new Thread()
{
   public void run()
   {
      MediaRecorder videoRecorder=new MediaRecorder();  
      videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//设置视频源   
      videoRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);//设置音频源
      videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//设置文件输出格式    
      videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//设置视频编码方式    
      videoRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//设置音频编码方式
      SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
      videoRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
 
      //设置录制的视频帧率,注意文档的说明:  
      //Must be called after setVideoSource().  
      //Call this after setOutFormat() but before prepare().  
      //videoRecorder.setVideoFrameRate(15);
 
      //设置输出路径  
      videoRecorder.setOutputFile("/mnt/sdcard/video.mp4");
 
      //设置视频高和宽,注意文档的说明:
      //Must be called after setVideoSource().  
      //Call this after setOutFormat() but before prepare().  
      //videoRecorder.setVideoSize(0xFFFF, 0xFFFF);
      videoRecorder.setMaxDuration(10000);//设置视频的最大持续时间
      videoRecorder.setMaxFileSize(1*1024*1024*1024);
      try {
         videoRecorder.prepare();
      } catch (Exception e) {
         e.printStackTrace();
      }
      videoRecorder.start();

      videoRecorder.setOnInfoListener(new OnInfoListener()
      {
         @Override
         public void onInfo(MediaRecorder recorder, int what, int extra)
         {
            switch(what)
            {
               case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
               {
                  if (recorder!=null)
                  {
                     recorder.stop();
                     recorder.release();
                     recorder=null;
                  }
               }
               break;



               case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
               {
                  if (recorder!=null)
                  {
                     recorder.stop();
                     recorder.release();
                     recorder=null;
                  }
               }
               break;
            }
         }
      });
   }
}.start(); 
 <SurfaceView
     android:id="@+id/surfaceView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/> 
<uses-permission android:name="android.permission.CAMERA" /><!-- 访问照像机的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 允许挂载和移除文件系统可移动存储器 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 允许写扩展存储器 --> 
<uses-permission android:name="android.permission.RECORD_AUDIO" /><!-- 录像权限 -->

 

上一篇:Android系统编程入门系列之硬件交互——多媒体麦克风


下一篇:web 视频流保存API MediaRecorder