模仿vine实现android视频的可间断录制和播放[basic]

http://blog.csdn.net/wodong/article/details/19761327

首先看下VINE在iphone上的界面, 这次我们主要实现的功能就是最左边这个界面。 

功能列表如下:

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

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

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


界面部分完全是copy vine的实现,包括其中的图片。 看下layout文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/solid_black"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <RelativeLayout  
  9.         android:id="@id/root_layout"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent" >  
  12.   
  13.         <RelativeLayout  
  14.             android:id="@id/top_mask"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="@dimen/capture_top_mask" >  
  17.   
  18.             <cn.wodong.capturevideo.ProgressView  
  19.                 android:id="@id/progress"  
  20.                 android:layout_width="fill_parent"  
  21.                 android:layout_height="48.0dip"  
  22.                 android:background="@color/bg_capture_progress" />  
  23.   
  24.             <LinearLayout  
  25.                 android:id="@id/cancel"  
  26.                 android:layout_width="56.0dip"  
  27.                 android:layout_height="fill_parent"  
  28.                 android:layout_alignParentLeft="true"  
  29.                 android:layout_centerVertical="true"  
  30.                 android:gravity="center"  
  31.                 android:onClick="onBackPressed" >  
  32.   
  33.                 <Button  
  34.                     android:layout_width="32.0dip"  
  35.                     android:layout_height="32.0dip"  
  36.                     android:background="@drawable/btn_capture_x"  
  37.                     android:onClick="onBackPressed" />  
  38.             </LinearLayout>  
  39.   
  40.             <LinearLayout  
  41.                 android:layout_width="56.0dip"  
  42.                 android:layout_height="fill_parent"  
  43.                 android:layout_centerInParent="true"  
  44.                 android:layout_centerVertical="true"  
  45.                 android:gravity="center" >  
  46.   
  47.                 <!--  
  48.                       TextView  
  49.                     android:id="@id/press"  
  50.                     android:layout_width="32.0dip"  
  51.                     android:layout_height="32.0dip"  
  52.                     android:background="@drawable/btn_capture_ghost">  
  53.                 -->  
  54.   
  55.                 <Button  
  56.                     android:id="@id/switchCamera"  
  57.                     android:layout_width="32.dip"  
  58.                     android:layout_height="32dip"  
  59.                     android:background="@drawable/btn_capture_front_facing_camera"  
  60.                     android:onClick="onCameraSwitchPressed" />  
  61.             </LinearLayout>  
  62.   
  63.             <LinearLayout  
  64.                 android:id="@id/finishLayOut"  
  65.                 android:layout_width="56.0dip"  
  66.                 android:layout_height="fill_parent"  
  67.                 android:layout_alignParentRight="true"  
  68.                 android:layout_centerVertical="true"  
  69.                 android:gravity="center"  
  70.                 android:onClick="onFinishPressed"  
  71.                 android:visibility="visible" >  
  72.   
  73.                 <Button  
  74.                     android:id="@id/finishButton"  
  75.                     android:layout_width="32.0dip"  
  76.                     android:layout_height="32.0dip"  
  77.                     android:background="@drawable/btn_capture_arrow"  
  78.                     android:onClick="onFinishPressed" />  
  79.             </LinearLayout>  
  80.   
  81.         </RelativeLayout>  
  82.   
  83.         <cn.wodong.capturevideo.MySurfaceView  
  84.             android:id="@id/cameraView"  
  85.             android:layout_width="fill_parent"  
  86.             android:layout_height="fill_parent"  
  87.             android:layout_below="@id/top_mask"  
  88.             android:layout_centerInParent="true" />  
  89.   
  90.         <VideoView  
  91.             android:id="@id/mediaShow"  
  92.             android:layout_width="fill_parent"  
  93.             android:layout_height="fill_parent"  
  94.             android:layout_below="@id/top_mask"  
  95.             android:layout_centerInParent="true" />  
  96.   
  97.         <RelativeLayout  
  98.             android:id="@id/bottom_mask"  
  99.             android:layout_width="fill_parent"  
  100.             android:layout_height="100.0dip"  
  101.             android:layout_alignParentBottom="true"  
  102.             android:layout_gravity="bottom"  
  103.             android:background="@color/capture_background" >  
  104.   
  105.             <LinearLayout  
  106.                 android:id="@id/camera_features"  
  107.                 style="@style/CameraFeatureLayout"  
  108.                 android:layout_width="fill_parent"  
  109.                 android:layout_height="51.0dip"  
  110.                 android:orientation="horizontal"  
  111.                 android:paddingTop="9.0dip" >  
  112.   
  113.                 <Button  
  114.                     android:layout_width="32.dip"  
  115.                     android:layout_height="32dip"  
  116.                     android:background="@drawable/btn_capture_front_facing_camera"  
  117.                     android:onClick="onCameraSwitchPressed"  
  118.                     android:visibility="invisible" />  
  119.             </LinearLayout>  
  120.         </RelativeLayout>  
  121.     </RelativeLayout>  
  122.   
  123. </RelativeLayout>  

启动相机:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     }  



进度条的实现:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     };  


录制视频参考实现:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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实现此功能,参考代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     }  


源码如下:


http://download.csdn.net/detail/wodong/6952273


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值