Android 间断录制和播放实现

来源:互联网 发布:java 进程创建原语 编辑:程序博客网 时间:2024/06/13 05:22

功能列表如下:

1. 触摸屏幕开始录制,松开后录制结束,接着触摸屏幕继续录制,直到录满6秒位置。

2. 录制完毕后,点击向右的箭头讲刚录制的视频进行播放。

3. 支持前后摄像头的切换。


layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="@color/solid_black"      tools:context=".MainActivity" >        <RelativeLayout          android:id="@id/root_layout"          android:layout_width="fill_parent"          android:layout_height="fill_parent" >            <RelativeLayout              android:id="@id/top_mask"              android:layout_width="fill_parent"              android:layout_height="@dimen/capture_top_mask" >                <cn.wodong.capturevideo.ProgressView                  android:id="@id/progress"                  android:layout_width="fill_parent"                  android:layout_height="48.0dip"                  android:background="@color/bg_capture_progress" />                <LinearLayout                  android:id="@id/cancel"                  android:layout_width="56.0dip"                  android:layout_height="fill_parent"                  android:layout_alignParentLeft="true"                  android:layout_centerVertical="true"                  android:gravity="center"                  android:onClick="onBackPressed" >                    <Button                      android:layout_width="32.0dip"                      android:layout_height="32.0dip"                      android:background="@drawable/btn_capture_x"                      android:onClick="onBackPressed" />              </LinearLayout>                <LinearLayout                  android:layout_width="56.0dip"                  android:layout_height="fill_parent"                  android:layout_centerInParent="true"                  android:layout_centerVertical="true"                  android:gravity="center" >                    <!--                        TextView                      android:id="@id/press"                      android:layout_width="32.0dip"                      android:layout_height="32.0dip"                      android:background="@drawable/btn_capture_ghost">                  -->                    <Button                      android:id="@id/switchCamera"                      android:layout_width="32.dip"                      android:layout_height="32dip"                      android:background="@drawable/btn_capture_front_facing_camera"                      android:onClick="onCameraSwitchPressed" />              </LinearLayout>                <LinearLayout                  android:id="@id/finishLayOut"                  android:layout_width="56.0dip"                  android:layout_height="fill_parent"                  android:layout_alignParentRight="true"                  android:layout_centerVertical="true"                  android:gravity="center"                  android:onClick="onFinishPressed"                  android:visibility="visible" >                    <Button                      android:id="@id/finishButton"                      android:layout_width="32.0dip"                      android:layout_height="32.0dip"                      android:background="@drawable/btn_capture_arrow"                      android:onClick="onFinishPressed" />              </LinearLayout>            </RelativeLayout>            <cn.wodong.capturevideo.MySurfaceView              android:id="@id/cameraView"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:layout_below="@id/top_mask"              android:layout_centerInParent="true" />            <VideoView              android:id="@id/mediaShow"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:layout_below="@id/top_mask"              android:layout_centerInParent="true" />            <RelativeLayout              android:id="@id/bottom_mask"              android:layout_width="fill_parent"              android:layout_height="100.0dip"              android:layout_alignParentBottom="true"              android:layout_gravity="bottom"              android:background="@color/capture_background" >                <LinearLayout                  android:id="@id/camera_features"                  style="@style/CameraFeatureLayout"                  android:layout_width="fill_parent"                  android:layout_height="51.0dip"                  android:orientation="horizontal"                  android:paddingTop="9.0dip" >                    <Button                      android:layout_width="32.dip"                      android:layout_height="32dip"                      android:background="@drawable/btn_capture_front_facing_camera"                      android:onClick="onCameraSwitchPressed"                      android:visibility="invisible" />              </LinearLayout>          </RelativeLayout>      </RelativeLayout>    </RelativeLayout>  

相机启动:

  1. public void startCamera(SurfaceHolder holder) {  
  2.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {  
  3.             for (int i = 0; i < camera.getNumberOfCameras(); i++) {  
  4.                 CameraInfo info = new CameraInfo();  
  5.                 Camera.getCameraInfo(i, info);  
  6.                 if (info.facing == cameraFacingType) {  
  7.                     camera = Camera.open(i);  
  8.                     break;  
  9.                 }  
  10.             }  
  11.         } else {  
  12.             camera = Camera.open();  
  13.         }  
  14.         Parameters parameters = camera.getParameters();  
  15.   
  16.         try {  
  17.             List<Size> sizeList = parameters.getSupportedPreviewSizes();  
  18.             int width = 0;  
  19.             for (Size s : sizeList) {  
  20.                 System.out.println(s.width + "," + s.height);  
  21.                 if (s.width > width && s.width <= 800) {  
  22.                     width = s.width;  
  23.                     defaultSize = s;  
  24.   
  25.                 }  
  26.             }  
  27.             camera.setDisplayOrientation(90);  
  28.             camera.setPreviewDisplay(holder);  
  29.         } catch (IOException e) {  
  30.             camera.release();  
  31.             camera = null;  
  32.         }  
  33.         try {  
  34.             parameters.setPreviewSize(640480);  
  35.             camera.setParameters(parameters);  
  36.             defaultSize = null;  
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.             parameters.setPreviewSize(defaultSize.width, defaultSize.height);  
  40.             camera.setParameters(parameters);  
  41.         }  
  42.   
  43.     }  


进度条实现:

  1. protected void onDraw(Canvas canvas) {  
  2.         super.onDraw(canvas);  
  3.         if (this.shouldBeWidth > 0) {  
  4.             canvas.drawRect(0.0F, 0.0F, this.shouldBeWidth,  
  5.                     getMeasuredHeight(), mPaint);  
  6.         }  
  7.     };  


录制视频:

  1. public void startRecord(boolean isFirst) {  
  2.         if (isMax) {  
  3.             return;  
  4.         }  
  5.         semp.acquireUninterruptibly();  
  6.         getCamera().stopPreview();  
  7.         mediaRecorder = new MediaRecorder();  
  8.         cameraManager.getCamera().unlock();  
  9.         mediaRecorder.setCamera(cameraManager.getCamera());  
  10.         if (cameraManager.isUseBackCamera()) {  
  11.             mediaRecorder.setOrientationHint(90);  
  12.         } else {  
  13.             mediaRecorder.setOrientationHint(90 + 180);  
  14.         }  
  15.         Size defaultSize = cameraManager.getDefaultSize();  
  16.   
  17.         mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);  
  18.         mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);  
  19.         mediaRecorder.setProfile(CamcorderProfile  
  20.                 .get(CameraProfile.QUALITY_HIGH));  
  21.         if (defaultSize != null) {  
  22.             mediaRecorder.setVideoSize(defaultSize.width, defaultSize.height);  
  23.         } else {  
  24.             mediaRecorder.setVideoSize(640480);  
  25.         }  
  26.         // camera.getParameters().setPictureSize(cameraSize.width,  
  27.         // cameraSize.height);  
  28.         // camera.setParameters(parameters);  
  29.         String fileName = parentPath + "/" + videoTempFiles.size() + ".mp4";  
  30.         mediaRecorder.setOutputFile(fileName);  
  31.         videoTempFiles.add(fileName);  
  32.         mediaRecorder.setPreviewDisplay(mySurfaceView.getHolder().getSurface());  
  33.         try {  
  34.             mediaRecorder.prepare();  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.             stopRecord();  
  38.         }  
  39.   
  40.         try {  
  41.             mediaRecorder.start();  
  42.             isStart = true;  
  43.             videoStartTime = new Date().getTime();  
  44.   
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.             if (isFirst) {  
  48.                 startRecord(false);  
  49.             } else {  
  50.                 stopRecord();  
  51.             }  
  52.         }  
  53.     }  


每次录制生成一个小的视频文件,最后要把所有的视频文件进行合并,合并成一个整体的视频文件。 采用了一个开源组件isoviewer实现此功能

  1. private void combineFiles() {  
  2.         try {  
  3.             List<Track> videoTracks = new LinkedList<Track>();  
  4.             List<Track> audioTracks = new LinkedList<Track>();  
  5.             for (String fileName : recorderManager.getVideoTempFiles()) {  
  6.                 try {  
  7.                     Movie movie = MovieCreator.build(fileName);  
  8.                     for (Track t : movie.getTracks()) {  
  9.                         if (t.getHandler().equals("soun")) {  
  10.                             audioTracks.add(t);  
  11.                         }  
  12.                         if (t.getHandler().equals("vide")) {  
  13.                             videoTracks.add(t);  
  14.                         }  
  15.                     }  
  16.                 } catch (Exception e) {  
  17.                     e.printStackTrace();  
  18.                 }  
  19.             }  
  20.   
  21.             Movie result = new Movie();  
  22.   
  23.             if (audioTracks.size() > 0) {  
  24.                 result.addTrack(new AppendTrack(audioTracks  
  25.                         .toArray(new Track[audioTracks.size()])));  
  26.             }  
  27.             if (videoTracks.size() > 0) {  
  28.                 result.addTrack(new AppendTrack(videoTracks  
  29.                         .toArray(new Track[videoTracks.size()])));  
  30.             }  
  31.   
  32.             Container out = new DefaultMp4Builder().build(result);  
  33.   
  34.             FileChannel fc = new RandomAccessFile(  
  35.                     String.format(getFinalVideoFileName()), "rw").getChannel();  
  36.             out.writeContainer(fc);  
  37.             fc.close();  
  38.         } catch (IOException e) {  
  39.             // TODO Auto-generated catch block  
  40.             e.printStackTrace();  
  41.         }  
  42.     }  


视频播放采用VideoView:

  1. public void startPlay() {  
  2.         combineFiles();  
  3.         recorderManager.reset();  
  4.         videoSurface.setVisibility(SurfaceView.GONE);  
  5.         videoView.setVisibility(SurfaceView.VISIBLE);  
  6.         videoView.setVideoPath(getFinalVideoFileName());  
  7.         videoView.start();  
  8.     }  


效果图:



0 0
原创粉丝点击