編輯:高級開發
Activity類:VideoActivity
- package com.media.Media;
- import Java.io.File;
- import Java.io.IOException;
- import android.app.Activity;
- import android.media.MediaRecorder;
- import android.os.Bundle;
- import android.os.Environment;
- import android.vIEw.SurfaceHolder;
- import android.view.SurfaceVIEw;
- import android.view.VIEw;
- import android.view.VIEw.OnClickListener;
- import android.widget.Button;
- public class VideoActivity extends Activity {
- private File myRecAudioFile;
- private SurfaceView mSurfaceVIEw;
- private SurfaceHolder mSurfaceHolder;
- private Button buttonStart;
- private Button buttonStop;
- private File dir;
- private MediaRecorder recorder;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentVIEw(R.layout.video);
- mSurfaceView = (SurfaceView) findViewById(R.id.videoVIEw);
- mSurfaceHolder = mSurfaceVIEw.getHolder();
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- buttonStart=(Button)findVIEwById(R.id.start);
- buttonStop=(Button)findVIEwById(R.id.stop);
- File defaultDir = Environment.getExternalStorageDirectory();
- String path = defaultDir.getAbsolutePath()+File.separator+"V"+File.separator;//創建文件夾存放視頻
- dir = new File(path);
- if(!dir.exists()){
- dir.mkdir();
- }
- recorder = new MediaRecorder();
- buttonStart.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(VIEw v) {
- recorder();
- }
- });
- buttonStop.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(VIEw v) {
- recorder.stop();
- recorder.reset();
- recorder.release();
- recorder=null;
- }
- });
- }
- public void recorder() {
- try {
- myRecAudioFile = File.createTempFile("video", ".3gp",dir);//創建臨時文件
- recorder.setPrevIEwDisplay(mSurfaceHolder.getSurface());//預覽
- recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//視頻源
- recorder.setAudioSource(MediaRecorder.AudiOSource.MIC); //錄音源為麥克風
- recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//輸出格式為3gp
- recorder.setVideoSize(800, 480);//視頻尺寸
- recorder.setVideoFrameRate(15);//視頻幀頻率
- recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);//視頻編碼
- recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音頻編碼
- recorder.setMaxDuration(10000);//最大期限
- recorder.setOutputFile(myRecAudioFile.getAbsolutePath());//保存路徑
- recorder.prepare();
- recorder.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
界面:video.XML
- <?XML version="1.0" encoding="utf-8"?>
- <LinearLayout
- XMLns:android="http://schemas.android.com/apk/res/android"
- android:orIEntation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <SurfaceVIEw android:id="@+id/videoVIEw"
- android:visibility="visible"
- android:layout_width="320px"
- android:layout_height="240px">
- </SurfaceVIEw>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="錄制"
- android:id="@+id/start"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/start"
- android:text="停止"
- android:id="@+id/stop"/>
- </RelativeLayout>
- </LinearLayout>
權限配置:androidManifest.XML
- <?XML version="1.0" encoding="utf-8"?>
- <manifest XMLns:android="http://schemas.android.com/apk/res/android"
- package="com.media.Media"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/rabbit" android:label="@string/app_name">
- <activity android:name=".VideoActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="7" />
- <uses-permission android:name="android.permission.CAMERA"/>
- <uses-permission android:name="android.permission.RECORD_AUDIO"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- </manifest>
我們以前給大家講過《android 2.2開發初學者快速入門十一大秘技》,Google目前通用版本便是android 2.2,代號“Froyo”,這個版本的進步非常大。
之前我們曾向您介紹過在android中實現service動態更新UI界面,在UI設計中需要利用很多圖庫相冊軟件,而Gallery 是國外一個免費開源的、功能非常強大、有
】本文我們將介紹一些利用android用戶自定義UI設計模板Dashboard的設計產品實例展示,讓大家更加直觀的了解android用戶設計模板——Dashboard。
下面文章主要說明一下android 源代碼的相關知識和簡單的操作技巧,為了讓大家更深的一步了解android 源代碼操作,這裡將用圖示的方法給廣的用戶講解學習,歡迎大家