Android 中使用MediaRecorder進行錄像詳解(視頻錄制) – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

在這裡給出自己的一個測試DEMO,裡面註釋很詳細。簡單的視頻錄制功能.

 

 

package com.video; 

 

import java.io.IOException; 

 

import android.app.Activity; 

import android.content.pm.ActivityInfo; 

import android.graphics.PixelFormat; 

import android.media.MediaRecorder; 

import android.os.Bundle; 

import android.view.SurfaceHolder; 

import android.view.SurfaceView; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.view.Window; 

import android.view.WindowManager; 

import android.widget.Button; 

 

/**

 * class name:TestBasicVideo<BR>

 * class description:一個簡單的錄制視頻例子<BR>

 * PS:實現基本的錄制保存文件<BR>

 * 

 * @version 1.00 2011/09/21

 * @author CODYY)peijiangping

 */ 

public class TestBasicVideo extends Activity implements SurfaceHolder.Callback { 

    private Button start;// 開始錄制按鈕 

    private Button stop;// 停止錄制按鈕 

    private MediaRecorder mediarecorder;// 錄制視頻的類 

    private SurfaceView surfaceview;// 顯示視頻的控件 

    // 用來顯示視頻的一個接口,我靠不用還不行,也就是說用mediarecorder錄制視頻還得給個界面看 

    // 想偷偷錄視頻的同學可以考慮別的辦法。。嗯需要實現這個接口的Callback接口 

    private SurfaceHolder surfaceHolder; 

 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄 

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 

                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 設置全屏 

        // 設置橫屏顯示 

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

        // 選擇支持半透明模式,在有surfaceview的activity中使用。 

        getWindow().setFormat(PixelFormat.TRANSLUCENT); 

        setContentView(R.layout.main); 

        init(); 

    } 

 

    private void init() { 

        start = (Button) this.findViewById(R.id.start); 

        stop = (Button) this.findViewById(R.id.stop); 

        start.setOnClickListener(new TestVideoListener()); 

        stop.setOnClickListener(new TestVideoListener()); 

        surfaceview = (SurfaceView) this.findViewById(R.id.surfaceview); 

        SurfaceHolder holder = surfaceview.getHolder();// 取得holder 

        holder.addCallback(this); // holder加入回調接口 

        // setType必須設置,要不出錯. 

        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    } 

 

    class TestVideoListener implements OnClickListener { 

 

        @Override 

        public void onClick(View v) { 

            if (v == start) { 

                mediarecorder = new MediaRecorder();// 創建mediarecorder對象 

                // 設置錄制視頻源為Camera(相機) 

                mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

                // 設置錄制完成後視頻的封裝格式THREE_GPP為3gp.MPEG_4為mp4 

                mediarecorder 

                        .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 

                // 設置錄制的視頻編碼h263 h264 

                mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 

                // 設置視頻錄制的分辨率。必須放在設置編碼和格式的後面,否則報錯 

                mediarecorder.setVideoSize(176, 144); 

                // 設置錄制的視頻幀率。必須放在設置編碼和格式的後面,否則報錯 

                mediarecorder.setVideoFrameRate(20); 

                mediarecorder.setPreviewDisplay(surfaceHolder.getSurface()); 

                // 設置視頻文件輸出的路徑 

                mediarecorder.setOutputFile("/sdcard/love.3gp"); 

                try { 

                    // 準備錄制 

                    mediarecorder.prepare(); 

                    // 開始錄制 

                    mediarecorder.start(); 

                } catch (IllegalStateException e) { 

                    // TODO Auto-generated catch block 

                    e.printStackTrace(); 

                } catch (IOException e) { 

                    // TODO Auto-generated catch block 

                    e.printStackTrace(); 

                } 

            } 

            if (v == stop) { 

                if (mediarecorder != null) { 

                    // 停止錄制 

                    mediarecorder.stop(); 

                    // 釋放資源 

                    mediarecorder.release(); 

                    mediarecorder = null; 

                } 

            } 

 

        } 

 

    } 

 

    @Override 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 

            int height) { 

        // 將holder,這個holder為開始在oncreat裡面取得的holder,將它賦給surfaceHolder 

        surfaceHolder = holder; 

    } 

 

    @Override 

    public void surfaceCreated(SurfaceHolder holder) { 

        // 將holder,這個holder為開始在oncreat裡面取得的holder,將它賦給surfaceHolder 

        surfaceHolder = holder; 

    } 

 

    @Override 

    public void surfaceDestroyed(SurfaceHolder holder) { 

        // surfaceDestroyed的時候同時對象設置為null 

        surfaceview = null; 

        surfaceHolder = null; 

        mediarecorder = null; 

    } 

 

main.xml

 

 

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="horizontal" > 

 

    <LinearLayout 

        android:layout_width="fill_parent" 

        android:layout_height="fill_parent" 

        android:layout_weight="1" > 

 

        <SurfaceView 

            android:id="@+id/surfaceview" 

            android:layout_width="fill_parent" 

            android:layout_height="fill_parent" /> 

    </LinearLayout> 

 

    <LinearLayout 

        android:layout_width="fill_parent" 

        android:layout_height="fill_parent" 

        android:layout_weight="4" 

        android:gravity="center" 

        android:orientation="vertical" > 

 

        <Button 

            android:id="@+id/start" 

            android:layout_width="fill_parent" 

            android:layout_height="wrap_content" 

            android:layout_weight="1" 

            android:text="Start" /> 

 

        <Button 

            android:id="@+id/stop" 

            android:layout_width="fill_parent" 

            android:layout_height="wrap_content" 

            android:layout_weight="1" 

            android:text="Stop" /> 

    </LinearLayout> 

 

</LinearLayout> 

 

AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="https://schemas.android.com/apk/res/android" 

    package="com.video" 

    android:versionCode="1" 

    android:versionName="1.0" > 

 

    <application 

        android:icon="@drawable/icon" 

        android:label="@string/app_name" > 

        <activity 

            android:label="@string/app_name" 

            android:name=".TestBasicVideo" > 

            <intent-filter > 

                <action android:name="android.intent.action.MAIN" /> 

 

                <category android:name="android.intent.category.LAUNCHER" /> 

            </intent-filter> 

        </activity> 

    </application> 

 

    <uses-sdk android:minSdkVersion="8" /> 

 

    <uses-permission android:name="android.permission.CAMERA" > 

    </uses-permission> 

    <uses-permission android:name="android.permission.RECORD_AUDIO" > 

    </uses-permission> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 

    </uses-permission> 

 

</manifest>   

 

摘自 裴裴的Android學習和分享

發佈留言