Android切換前後置攝像頭並錄製視訊

查志強發表於2014-07-13

【原文:http://blog.csdn.net/cuiran/article/details/12779005

專案需要對微信的視訊模組也看了一下,在此就對這塊進行了一個開發。首先給出效果圖

 

首先給出java程式碼

[java] view plaincopy
  1. /** 
  2.  * RecordActivity.java 
  3.  * 版權所有(C) 2013  
  4.  * 建立:cuiran 2013-10-15 下午3:20:45 
  5.  */  
  6. package com.app931.oa.activity.video;  
  7.   
  8. import java.io.IOException;  
  9. import java.util.List;  
  10.   
  11. import com.app931.oa.R;  
  12. import com.app931.oa.util.LogsUtil;  
  13. import com.app931.oa.util.SupportedSizesReflect;  
  14.   
  15. import android.app.Activity;  
  16. import android.content.Context;  
  17.   
  18. import android.graphics.PixelFormat;  
  19. import android.hardware.Camera;  
  20. import android.hardware.Camera.CameraInfo;  
  21. import android.hardware.Camera.Size;  
  22. import android.media.AudioManager;  
  23. import android.media.MediaPlayer;  
  24. import android.media.MediaPlayer.OnBufferingUpdateListener;  
  25. import android.media.MediaPlayer.OnCompletionListener;  
  26. import android.media.MediaRecorder;  
  27. import android.os.Bundle;  
  28. import android.util.DisplayMetrics;  
  29. import android.view.Display;  
  30. import android.view.SurfaceHolder;  
  31. import android.view.SurfaceView;  
  32. import android.view.View;  
  33. import android.view.WindowManager;  
  34. import android.view.SurfaceHolder.Callback;  
  35. import android.widget.Button;  
  36. import android.widget.ImageView;  
  37. import android.widget.TextView;  
  38.   
  39. /** 
  40.  * TODO 
  41.  * @author cuiran 
  42.  * @version 1.0.0 
  43.  */  
  44. public class RecordActivity extends Activity implements Callback,MediaPlayer.OnPreparedListener, OnBufferingUpdateListener,   
  45. OnCompletionListener  
  46.  {  
  47.     private static final String TAG="RecordActivity";  
  48.     private String videoPath="/sdcard/love.3gp";  
  49.       
  50.     private MediaRecorder mediarecorder;// 錄製視訊的類  
  51.     private MediaPlayer mediaPlayer;//播放視訊的類  
  52.     private SurfaceView surfaceview;// 顯示視訊的控制元件  
  53.     private  Camera camera;  
  54.     //實現這個介面的Callback介面  
  55.     private SurfaceHolder surfaceHolder;  
  56.     /** 
  57.      * 是否正在錄製true錄製中 false未錄製 
  58.      */  
  59.     private boolean isRecord=false;  
  60.       
  61.     public boolean isCameraBack=true;  
  62.       
  63.     private ImageView recordIv;  
  64.     private ImageView recordPlayIv;  
  65.       
  66.     private int mVideoWidth;  
  67.     private int mVideoHeight;  
  68.     int cameraCount = 0;  
  69.       
  70.     private int cameraPosition = 1;//0代表前置攝像頭,1代表後置攝像頭  
  71.   
  72.   
  73.     public void onCreate(Bundle savedInstanceState) {  
  74.         super.onCreate(savedInstanceState);  
  75.         setContentView(R.layout.layout_video_record1);  
  76.         setTitleStr("");  
  77.   
  78.                 // 選擇支援半透明模式,在有surfaceview的activity中使用。  
  79.         getWindow().setFormat(PixelFormat.TRANSLUCENT);  
  80.               
  81.         initView();  
  82.   
  83.           
  84.     }  
  85.       
  86.   
  87.     /** 
  88.      * TODO 
  89.      */  
  90.     private void initView() {  
  91.         // TODO Auto-generated method stub  
  92.           
  93.         surfaceview = (SurfaceView) this.findViewById(R.id.surfaceview);  
  94.         recordIv=(ImageView)findViewById(R.id.recordIv);  
  95.         recordPlayIv=(ImageView)findViewById(R.id.recordPlayIv);  
  96.           
  97.         SurfaceHolder holder = surfaceview.getHolder();// 取得holder  
  98.         holder.addCallback(this); // holder加入回撥介面  
  99.         // setType必須設定,要不出錯.  
  100.         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  101.           
  102.           
  103.     }  
  104.     /** 
  105.      * 播放視訊 
  106.      * TODO 
  107.      * @param v 
  108.      */  
  109.     public void playVideo(View v){  
  110.         recordPlayIv.setVisibility(View.GONE);  
  111.         try {  
  112.             mediaPlayer=new MediaPlayer();  
  113.             mediaPlayer.setDataSource(videoPath);  
  114.             mediaPlayer.setDisplay(surfaceHolder);  
  115.             mediaPlayer.prepareAsync();  
  116.               
  117.         } catch (IllegalArgumentException e) {  
  118.             // TODO Auto-generated catch block         
  119.             e.printStackTrace();  
  120.         } catch (IllegalStateException e) {  
  121.             // TODO Auto-generated catch block  
  122.             e.printStackTrace();  
  123.         } catch (IOException e) {  
  124.             // TODO Auto-generated catch block  
  125.             e.printStackTrace();  
  126.         }  
  127.           
  128.         mediaPlayer.setOnBufferingUpdateListener(this);  
  129.         mediaPlayer.setOnCompletionListener(this);  
  130.         mediaPlayer.setOnPreparedListener(this);  
  131.         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
  132.   
  133.     }  
  134.       
  135.     /** 
  136.      * 開始錄製/停止錄製 
  137.      * TODO 
  138.      * @param v 
  139.      */  
  140.     public void recordVideo(View v){  
  141.         if(isRecord){  
  142.             isRecord=false;  
  143.             recordIv.setImageResource(R.drawable.video_recorder_start_btn_nor);  
  144.             recordPlayIv.setVisibility(View.VISIBLE);  
  145.             if (mediarecorder != null) {  
  146.                 // 停止錄製  
  147.                 mediarecorder.stop();  
  148.                 // 釋放資源  
  149.                 mediarecorder.release();  
  150.                 mediarecorder = null;  
  151.             }  
  152.             if(camera!=null){  
  153.                 camera.release();  
  154.             }  
  155.         }else{  
  156.             isRecord=true;  
  157.             recordIv.setImageResource(R.drawable.video_recorder_recording_btn);  
  158.             recordPlayIv.setVisibility(View.GONE);  
  159.             mediarecorder = new MediaRecorder();// 建立mediarecorder物件  
  160. //          // 從麥克風源進行錄音   
  161. //          mediarecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);   
  162. //          // 設定輸出格式   
  163. //          mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);   
  164. //          // 設定編碼格式   
  165. //          mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  
  166.             /** 
  167.              * 設定豎著錄製 
  168.              */  
  169.             if(camera!=null){  
  170.                 camera.release();  
  171.             }  
  172.               
  173.             if(cameraPosition==1){  
  174.                 camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);//開啟攝像頭  
  175. //               camera = Camera.open(cameraPosition);//開啟攝像頭  
  176. //               Camera.Parameters parameters = camera.getParameters();  
  177. //               camera.setDisplayOrientation(90);  
  178.                      
  179.                  
  180. //               camera.setParameters(parameters);   
  181.                    
  182.                  camera=deal(camera);  
  183.                  mediarecorder.setOrientationHint(90);//視訊旋轉90度  
  184.                
  185.                 
  186.             }else{  
  187.                 camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);//開啟攝像頭  
  188.                  Camera.Parameters parameters = camera.getParameters();  
  189.                  camera.setDisplayOrientation(90);  
  190.                  camera.setParameters(parameters);   
  191.                 mediarecorder.setOrientationHint(270);//視訊旋轉90度  
  192.             }  
  193.               
  194.             camera.unlock();  
  195.               
  196.             mediarecorder.setCamera(camera);  
  197.               
  198.             // 設定錄製視訊源為Camera(相機)  
  199.             mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);  
  200.             // 設定錄製完成後視訊的封裝格式THREE_GPP為3gp.MPEG_4為mp4  
  201.             mediarecorder  
  202.                     .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
  203.             // 設定錄製的視訊編碼h263 h264  
  204.             mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);  
  205.             // 設定視訊錄製的解析度。必須放在設定編碼和格式的後面,否則報錯  
  206.             mediarecorder.setVideoSize(176144);  
  207.             // 設定錄製的視訊幀率。必須放在設定編碼和格式的後面,否則報錯  
  208.             mediarecorder.setVideoFrameRate(20);  
  209.           
  210.               
  211.             mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());  
  212.             // 設定視訊檔案輸出的路徑  
  213.             mediarecorder.setOutputFile(videoPath);  
  214.             try {  
  215.                 // 準備錄製  
  216.                 mediarecorder.prepare();  
  217.                 mediarecorder.start();  
  218.             } catch (IllegalStateException e) {  
  219.                 // TODO Auto-generated catch block  
  220.                 e.printStackTrace();  
  221.             } catch (IOException e) {  
  222.                 // TODO Auto-generated catch block  
  223.                 e.printStackTrace();  
  224.             }  
  225.               
  226.         }  
  227.     }  
  228.   
  229.     protected void setTitleStr(String str) {  
  230.             TextView titleText = (TextView) findViewById(R.id.common_title_text);  
  231.             titleText.setText(str.trim());  
  232.               
  233.             Button left_button=(Button)findViewById(R.id.left_button);  
  234.             left_button.setVisibility(View.VISIBLE);  
  235.             left_button.setText("返回");  
  236.             left_button.setOnClickListener(new View.OnClickListener() {  
  237.                   
  238.                 @Override  
  239.                 public void onClick(View v) {  
  240.                     // TODO Auto-generated method stub  
  241.                     back();  
  242.                 }  
  243.             });  
  244.             Button right_button=(Button)findViewById(R.id.right_button);  
  245.             right_button.setVisibility(View.VISIBLE);  
  246.             right_button.setText("");  
  247.             right_button.setBackgroundResource(R.drawable.btn_video_switch_bg);  
  248.             right_button.setOnClickListener(new View.OnClickListener() {  
  249.                   
  250.                 @Override  
  251.                 public void onClick(View v) {  
  252.                     // TODO Auto-generated method stub  
  253.                     cameraCount=Camera.getNumberOfCameras();  
  254.                     if(isCameraBack){  
  255.                         isCameraBack=false;  
  256.                     }else{  
  257.                         isCameraBack=true;  
  258.                     }  
  259. //                  SurfaceHolder holder = surfaceview.getHolder();// 取得holder  
  260. //                  holder.addCallback(RecordActivity.this); // holder加入回撥介面  
  261. //                  LogsUtil.i(TAG, "cameraCount="+cameraCount);  
  262.                       
  263.                     int cameraCount = 0;  
  264.                     CameraInfo cameraInfo = new CameraInfo();  
  265.                     cameraCount = Camera.getNumberOfCameras();//得到攝像頭的個數  
  266.   
  267.                     for(int i = 0; i < cameraCount; i++) {  
  268.                           
  269.                         Camera.getCameraInfo(i, cameraInfo);//得到每一個攝像頭的資訊  
  270.                         if(cameraPosition == 1) {  
  271.                             //現在是後置,變更為前置  
  272.                             if(cameraInfo.facing  == Camera.CameraInfo.CAMERA_FACING_FRONT) {//代表攝像頭的方位,CAMERA_FACING_FRONT前置      CAMERA_FACING_BACK後置    
  273.                                   
  274.                                 camera.stopPreview();//停掉原來攝像頭的預覽  
  275.                                 camera.release();//釋放資源  
  276.                                 camera = null;//取消原來攝像頭  
  277.                                 camera = Camera.open(i);//開啟當前選中的攝像頭  
  278.                                 try {  
  279.                                     deal(camera);  
  280.                                     camera.setPreviewDisplay(surfaceHolder);//通過surfaceview顯示取景畫面  
  281.                                 } catch (IOException e) {  
  282.                                     // TODO Auto-generated catch block  
  283.                                     e.printStackTrace();  
  284.                                 }  
  285.                                 camera.startPreview();//開始預覽  
  286.                                 cameraPosition = 0;  
  287.                                 break;  
  288.                             }  
  289.                         } else {  
  290.                             //現在是前置, 變更為後置  
  291.                             if(cameraInfo.facing  == Camera.CameraInfo.CAMERA_FACING_BACK) {//代表攝像頭的方位,CAMERA_FACING_FRONT前置      CAMERA_FACING_BACK後置    
  292.                                 camera.stopPreview();//停掉原來攝像頭的預覽  
  293.                                 camera.release();//釋放資源  
  294.                                 camera = null;//取消原來攝像頭  
  295.                                 camera = Camera.open(i);//開啟當前選中的攝像頭  
  296.                                 try {  
  297.                                     deal(camera);  
  298.                                     camera.setPreviewDisplay(surfaceHolder);//通過surfaceview顯示取景畫面  
  299.                                 } catch (IOException e) {  
  300.                                     // TODO Auto-generated catch block  
  301.                                     e.printStackTrace();  
  302.                                 }  
  303.                                 camera.startPreview();//開始預覽  
  304.                                 cameraPosition = 1;  
  305.                                 break;  
  306.                             }  
  307.                         }  
  308.   
  309.                     }  
  310.                       
  311.                 }  
  312.             });  
  313.               
  314.      }  
  315.       
  316.      /** 
  317.       * 返回 
  318.       *<b>function:</b> 
  319.       *@author cuiran 
  320.       *@createDate 2013-8-20 下午2:22:48 
  321.       */  
  322.      public void back(){  
  323.               
  324.         finish();  
  325.           
  326.           
  327.      }  
  328.   
  329.      public Camera deal(Camera camera){  
  330.             //設定camera預覽的角度,因為預設圖片是傾斜90度的     
  331.             camera.setDisplayOrientation(90);   
  332.           
  333.             Size pictureSize=null;  
  334.             Size previewSize=null;  
  335.             Camera.Parameters parameters = camera.getParameters();  
  336.             parameters.setPreviewFrameRate(5);  
  337.             //設定旋轉程式碼  
  338.             parameters.setRotation(90);    
  339. //          parameters.setPictureFormat(PixelFormat.JPEG);  
  340.               
  341.             List<Size> supportedPictureSizes  
  342.             = SupportedSizesReflect.getSupportedPictureSizes(parameters);  
  343.             List<Size> supportedPreviewSizes  
  344.             = SupportedSizesReflect.getSupportedPreviewSizes(parameters);  
  345.   
  346.             if ( supportedPictureSizes != null &&  
  347.                     supportedPreviewSizes != null &&  
  348.                     supportedPictureSizes.size() > 0 &&  
  349.                     supportedPreviewSizes.size() > 0) {  
  350.   
  351.                     //2.x  
  352.                     pictureSize = supportedPictureSizes.get(0);  
  353.   
  354.                     int maxSize = 1280;  
  355.                     if(maxSize > 0){  
  356.                         for(Size size : supportedPictureSizes){  
  357.                             if(maxSize >= Math.max(size.width,size.height)){  
  358.                                 pictureSize = size;  
  359.                                 break;  
  360.                             }  
  361.                         }  
  362.                     }  
  363.   
  364.                     WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
  365.                     Display display = windowManager.getDefaultDisplay();  
  366.                     DisplayMetrics displayMetrics = new DisplayMetrics();  
  367.                     display.getMetrics(displayMetrics);  
  368.   
  369.                     previewSize = getOptimalPreviewSize(  
  370.                                         supportedPreviewSizes,  
  371.                                         display.getWidth(),  
  372.                                         display.getHeight());   
  373.   
  374.                     parameters.setPictureSize(pictureSize.width, pictureSize.height);  
  375.                     parameters.setPreviewSize(previewSize.width, previewSize.height);                                 
  376.   
  377.                 }  
  378.             camera.setParameters(parameters);  
  379.             return camera;  
  380.      }  
  381.     /* (non-Javadoc) 
  382.      * @see android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int) 
  383.      */  
  384.     @Override  
  385.     public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2, int arg3) {  
  386.         // TODO Auto-generated method stub  
  387.         // 將holder,這個holder為開始在oncreat裡面取得的holder,將它賦給surfaceHolder  
  388.         surfaceHolder = holder;  
  389.     }  
  390.     private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {  
  391.         final double ASPECT_TOLERANCE = 0.1;  
  392.         double targetRatio = (double) w / h;  
  393.         if (sizes == nullreturn null;  
  394.   
  395.         Size optimalSize = null;  
  396.         double minDiff = Double.MAX_VALUE;  
  397.   
  398.         int targetHeight = h;  
  399.   
  400.         // Try to find an size match aspect ratio and size  
  401.         for (Size size : sizes) {  
  402.             double ratio = (double) size.width / size.height;  
  403.             if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;  
  404.             if (Math.abs(size.height - targetHeight) < minDiff) {  
  405.                 optimalSize = size;  
  406.                 minDiff = Math.abs(size.height - targetHeight);  
  407.             }  
  408.         }  
  409.   
  410.         // Cannot find the one match the aspect ratio, ignore the requirement  
  411.         if (optimalSize == null) {  
  412.             minDiff = Double.MAX_VALUE;  
  413.             for (Size size : sizes) {  
  414.                 if (Math.abs(size.height - targetHeight) < minDiff) {  
  415.                     optimalSize = size;  
  416.                     minDiff = Math.abs(size.height - targetHeight);  
  417.                 }  
  418.             }  
  419.         }  
  420.         return optimalSize;  
  421.     }  
  422.     /* (non-Javadoc) 
  423.      * @see android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder) 
  424.      */  
  425.     @Override  
  426.     public void surfaceCreated(SurfaceHolder holder) {  
  427.         // TODO Auto-generated method stub  
  428.         surfaceHolder = holder;  
  429.           
  430.         try {  
  431.             if(isCameraBack){  
  432.                 camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);//開啟攝像頭  
  433.                   
  434.             }else{  
  435.                 camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);//開啟攝像頭  
  436.                   
  437.             }  
  438.                
  439.             //設定camera預覽的角度,因為預設圖片是傾斜90度的     
  440.             camera.setDisplayOrientation(90);   
  441.           
  442.             Size pictureSize=null;  
  443.             Size previewSize=null;  
  444.             Camera.Parameters parameters = camera.getParameters();  
  445.             parameters.setPreviewFrameRate(5);  
  446.             //設定旋轉程式碼  
  447.             parameters.setRotation(90);    
  448. //          parameters.setPictureFormat(PixelFormat.JPEG);  
  449.               
  450.             List<Size> supportedPictureSizes  
  451.             = SupportedSizesReflect.getSupportedPictureSizes(parameters);  
  452.             List<Size> supportedPreviewSizes  
  453.             = SupportedSizesReflect.getSupportedPreviewSizes(parameters);  
  454.   
  455.             if ( supportedPictureSizes != null &&  
  456.                     supportedPreviewSizes != null &&  
  457.                     supportedPictureSizes.size() > 0 &&  
  458.                     supportedPreviewSizes.size() > 0) {  
  459.   
  460.                     //2.x  
  461.                     pictureSize = supportedPictureSizes.get(0);  
  462.   
  463.                     int maxSize = 1280;  
  464.                     if(maxSize > 0){  
  465.                         for(Size size : supportedPictureSizes){  
  466.                             if(maxSize >= Math.max(size.width,size.height)){  
  467.                                 pictureSize = size;  
  468.                                 break;  
  469.                             }  
  470.                         }  
  471.                     }  
  472.   
  473.                     WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
  474.                     Display display = windowManager.getDefaultDisplay();  
  475.                     DisplayMetrics displayMetrics = new DisplayMetrics();  
  476.                     display.getMetrics(displayMetrics);  
  477.   
  478.                     previewSize = getOptimalPreviewSize(  
  479.                                         supportedPreviewSizes,  
  480.                                         display.getWidth(),  
  481.                                         display.getHeight());   
  482.   
  483.                     parameters.setPictureSize(pictureSize.width, pictureSize.height);  
  484.                     parameters.setPreviewSize(previewSize.width, previewSize.height);                                 
  485.   
  486.                 }  
  487.             camera.setParameters(parameters);  
  488.             camera.setPreviewDisplay(holder);  
  489.             camera.startPreview();  
  490.         } catch (Exception e) {  
  491.             e.printStackTrace();  
  492.         }  
  493.           
  494.     }  
  495.   
  496.     /* (non-Javadoc) 
  497.      * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder) 
  498.      */  
  499.     @Override  
  500.     public void surfaceDestroyed(SurfaceHolder arg0) {  
  501.         // TODO Auto-generated method stub  
  502.         if(camera!=null){  
  503.             camera.release();  
  504.         }  
  505.         surfaceview = null;  
  506.         surfaceHolder = null;  
  507.         if (surfaceHolder != null) {  
  508.             surfaceHolder=null;  
  509.         }  
  510.         if (mediarecorder != null) {  
  511.             mediarecorder=null;  
  512.         }  
  513.          if (mediaPlayer != null) {  
  514.              mediaPlayer.release();  
  515.              mediaPlayer = null;  
  516.          }  
  517.   
  518.     }  
  519.   
  520.     /* (non-Javadoc) 
  521.      * @see android.media.MediaPlayer.OnCompletionListener#onCompletion(android.media.MediaPlayer) 
  522.      */  
  523.     @Override  
  524.     public void onCompletion(MediaPlayer arg0) {  
  525.         // TODO Auto-generated method stub  
  526.          LogsUtil.i(TAG, "onCompletion");  
  527.          if (mediaPlayer != null) {  
  528.              mediaPlayer.release();  
  529.              mediaPlayer = null;  
  530.          }  
  531.          recordPlayIv.setVisibility(View.VISIBLE);  
  532.     }  
  533.   
  534.     /* (non-Javadoc) 
  535.      * @see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer) 
  536.      */  
  537.     @Override  
  538.     public void onPrepared(MediaPlayer arg0) {  
  539.         // TODO Auto-generated method stub  
  540.         mVideoWidth = mediaPlayer.getVideoWidth();  
  541.         mVideoHeight = mediaPlayer.getVideoHeight();  
  542.           if (mVideoWidth != 0 && mVideoHeight != 0)  
  543.           {  
  544.                
  545.            /* 設定視訊的寬度和高度 */  
  546.            surfaceHolder.setFixedSize(mVideoWidth,mVideoHeight);  
  547.             
  548.            /* 開始播放 */  
  549.            mediaPlayer.start();  
  550.           }  
  551.     }  
  552.   
  553.     /* (non-Javadoc) 
  554.      * @see android.media.MediaPlayer.OnBufferingUpdateListener#onBufferingUpdate(android.media.MediaPlayer, int) 
  555.      */  
  556.     @Override  
  557.     public void onBufferingUpdate(MediaPlayer player, int arg1) {  
  558.         // TODO Auto-generated method stub  
  559.           
  560.     }  
  561.   
  562. }  


然後給出佈局檔案程式碼

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout android:orientation="vertical"  
  3.     android:layout_width="fill_parent" android:background="@drawable/login_bj"  
  4.     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">  
  5.   
  6.   <include android:id="@+id/Title"  
  7.              android:layout_alignParentTop="true"  
  8.              android:layout_width="fill_parent"  
  9.              android:layout_height="wrap_content"  
  10.              layout="@layout/layout_common_title"/>  
  11.    
  12.    
  13.   <LinearLayout  
  14.        android:layout_below="@+id/Title"  
  15.        android:layout_above="@+id/recordBtn"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="fill_parent"  
  18.         android:padding="30dp"  
  19.         >  
  20.         <SurfaceView  
  21.             android:id="@+id/surfaceview"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="fill_parent" />  
  24.          
  25.           
  26.     </LinearLayout>  
  27.     
  28.   <RelativeLayout   
  29.     android:id="@+id/videoPlay"  
  30.     android:layout_width="fill_parent"  
  31.      android:layout_height="fill_parent"  
  32.       android:gravity="center"  
  33.       >  
  34.     <ImageView   
  35.       android:src="@drawable/btn_video_play_bg"  
  36.       android:id="@+id/recordPlayIv"  
  37.       android:layout_width="wrap_content"  
  38.       android:layout_height="wrap_content"  
  39.       android:clickable="true"  
  40.       android:onClick="playVideo"  
  41.       android:visibility="gone"  
  42.      />  
  43.   </RelativeLayout>  
  44.     
  45. <RelativeLayout   
  46.     android:id="@+id/recordBtn"  
  47.     android:layout_width="fill_parent"  
  48.       android:layout_height="wrap_content"  
  49.       android:gravity="center"  
  50.       android:layout_alignParentBottom="true">  
  51.      <ImageView   
  52.       android:src="@drawable/video_recorder_start_btn_nor"  
  53.       android:id="@+id/recordIv"  
  54.       android:layout_width="wrap_content"  
  55.       android:layout_height="wrap_content"  
  56.       android:clickable="true"  
  57.       android:onClick="recordVideo"  
  58.      />  
  59. </RelativeLayout>  
  60.    
  61.     
  62. </RelativeLayout>  


這裡還有一些地方需要完善,在錄製完成後切換會出現問題。由於時間關係這邊還沒去處理,在後續會進行處理,

此程式碼採用的sdk是17,由於在android 2.2上錄製視訊是橫著,所以採用了高點的sdk


相關文章