Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android視錄視頻示例及代碼下載

Android視錄視頻示例及代碼下載

編輯:高級開發

幾天需要搞一個android視頻通話功能,從最簡單的視頻錄制開始,網上例子大多不完整。下面的示例參考過別人的代碼,還是拿出來給需要的朋友分享下。

Activity類:VideoActivity

  1. package com.media.Media;
  2. import Java.io.File;
  3. import Java.io.IOException;
  4. import android.app.Activity;
  5. import android.media.MediaRecorder;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.vIEw.SurfaceHolder;
  9. import android.view.SurfaceVIEw;
  10. import android.view.VIEw;
  11. import android.view.VIEw.OnClickListener;
  12. import android.widget.Button;
  13. public class VideoActivity extends Activity {
  14. private File myRecAudioFile;
  15. private SurfaceView mSurfaceVIEw;
  16. private SurfaceHolder mSurfaceHolder;
  17. private Button buttonStart;
  18. private Button buttonStop;
  19. private File dir;
  20. private MediaRecorder recorder;
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentVIEw(R.layout.video);
  25. mSurfaceView = (SurfaceView) findViewById(R.id.videoVIEw);
  26. mSurfaceHolder = mSurfaceVIEw.getHolder();
  27. mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  28. buttonStart=(Button)findVIEwById(R.id.start);
  29. buttonStop=(Button)findVIEwById(R.id.stop);
  30. File defaultDir = Environment.getExternalStorageDirectory();
  31. String path = defaultDir.getAbsolutePath()+File.separator+"V"+File.separator;//創建文件夾存放視頻
  32. dir = new File(path);
  33. if(!dir.exists()){
  34. dir.mkdir();
  35. }
  36. recorder = new MediaRecorder();
  37. buttonStart.setOnClickListener(new OnClickListener() {
  38. @Override
  39. public void onClick(VIEw v) {
  40. recorder();
  41. }
  42. });
  43. buttonStop.setOnClickListener(new OnClickListener() {
  44. @Override
  45. public void onClick(VIEw v) {
  46. recorder.stop();
  47. recorder.reset();
  48. recorder.release();
  49. recorder=null;
  50. }
  51. });
  52. }
  53. public void recorder() {
  54. try {
  55. myRecAudioFile = File.createTempFile("video", ".3gp",dir);//創建臨時文件
  56. recorder.setPrevIEwDisplay(mSurfaceHolder.getSurface());//預覽
  57. recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//視頻源
  58. recorder.setAudioSource(MediaRecorder.AudiOSource.MIC); //錄音源為麥克風
  59. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//輸出格式為3gp
  60. recorder.setVideoSize(800, 480);//視頻尺寸
  61. recorder.setVideoFrameRate(15);//視頻幀頻率
  62. recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);//視頻編碼
  63. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音頻編碼
  64. recorder.setMaxDuration(10000);//最大期限
  65. recorder.setOutputFile(myRecAudioFile.getAbsolutePath());//保存路徑
  66. recorder.prepare();
  67. recorder.start();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }

界面:video.XML

  1. <?XML version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. XMLns:android="http://schemas.android.com/apk/res/android"
  4. android:orIEntation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <SurfaceVIEw android:id="@+id/videoVIEw"
  8. android:visibility="visible"
  9. android:layout_width="320px"
  10. android:layout_height="240px">
  11. </SurfaceVIEw>
  12. <RelativeLayout
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content">
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="錄制"
  19. android:id="@+id/start"/>
  20. <Button
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_toRightOf="@id/start"
  24. android:text="停止"
  25. android:id="@+id/stop"/>
  26. </RelativeLayout>
  27. </LinearLayout>

權限配置:androidManifest.XML

  1. <?XML version="1.0" encoding="utf-8"?>
  2. <manifest XMLns:android="http://schemas.android.com/apk/res/android"
  3. package="com.media.Media"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/rabbit" android:label="@string/app_name">
  7. <activity android:name=".VideoActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-sdk android:minSdkVersion="7" />
  16. <uses-permission android:name="android.permission.CAMERA"/>
  17. <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  18. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  19. </manifest>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved