閱讀154 返回首頁    go 阿裏雲 go 技術社區[雲棲]


音視頻采集

原文:https://www.cnblogs.com/tjpfly/archive/2011/06/08/2074735.html


第一步:在功能清單文件AndroidManifest.xml中添加音頻刻錄和照相機權限:

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

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

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

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

第二步:編寫音頻刻錄代碼:

recorder.reset();

recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //從照相機采集視頻

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

recorder.setVideoSize(320, 240);

recorder.setVideoFrameRate(3); //每秒3幀

recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //設置視頻編碼方式

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

recorder.setOutputFile("/mnt/sdcard/itcast.3gp");

recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

recorder.prepare();//預期準備

recorder.start();//開始刻錄

...

recorder.stop();//停止刻錄

recorder.release(); //刻錄完成一定要釋放資源



strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, VideoRecordActivity!</string>
    <string name="app_name">視頻刻錄</string>
    <string name="record">刻錄</string>
    <string name="stop">停止</string>
    <string name="success">刻錄完成</string>
    <string name="error">刻錄失敗</string>
    <string name="sdcarderror">SDCARD不存在或者寫保護</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
      package="com.tjp.videorecord"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" >
        <activity android:name=".VideoRecordActivity"
                  android:label="@string/app_name" android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <!-- 音頻權限 -->
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <!-- 視頻權限 -->
    <uses-permission android:name="android.permission.CAMERA"/>
    <!-- sdcard權限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >
<SurfaceView  
    android:layout_width="fill_parent" 
    android:layout_height="220dip" 
    android:
    />
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="right"
    >
    <Button  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:
    android:text="@string/record"
    />
    <Button  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dip"
    android:
    android:text="@string/stop"
    />
</LinearLayout>
</LinearLayout>

VideoRecordActivity

package com.tjp.videorecord;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class VideoRecordActivity extends Activity {
    
    private SurfaceView surfaceView;
    private MediaRecorder recorder; 
    private boolean record=false;
    private static final String TAG="VideoRecordActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        surfaceView=(SurfaceView)findViewById(R.id.surfaceview);
        Button stopButton=(Button)findViewById(R.id.stop);
        Button recordButton=(Button)findViewById(R.id.record);
        ButtonClickListiner buttonClickListiner=new ButtonClickListiner();
        stopButton.setOnClickListener(buttonClickListiner);
        recordButton.setOnClickListener(buttonClickListiner);
        /*下麵設置Surface不維護自己的緩衝區,而是等待屏幕的渲染引擎將內容推送到用戶麵前*/
        this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.surfaceView.getHolder().setFixedSize(320, 240);//設置分辨率
        this.surfaceView.getHolder().setKeepScreenOn(true);

    }
    
    @Override
    protected void onDestroy() {
        recorder.release();
        super.onDestroy();
    }

    private final class ButtonClickListiner implements OnClickListener{

        @Override
        public void onClick(View v) {
            if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                Toast.makeText(VideoRecordActivity.this, R.string.sdcarderror, Toast.LENGTH_SHORT).show();
            }
            try {
                switch (v.getId()) {
                case R.id.stop:
                    if(record){
                        recorder.stop();
                        record=false;
                    }
                    break;
                case R.id.record:
                    recorder.reset();                
                    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //從照相機采集視頻
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);             
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setVideoSize(320, 240);
                    recorder.setVideoFrameRate(3); //每秒3幀            
                    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //設置視頻編碼方式
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);    
                    recorder.setOutputFile("/mnt/sdcard/tjp.3gp");            
                    recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
                    recorder.prepare();//預期準備
                    recorder.start();//開始刻錄
                    record=true;
                    break;

                default:
                    break;
                }
            } catch (Exception e) {
                Log.i(TAG, e.getMessage());
                Toast.makeText(VideoRecordActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
            }
            
        }
        
    }
}


最後更新:2017-04-02 06:52:02

  上一篇:go C/C++基本類型字節
  下一篇:go 21世紀普通高等教育應用型規劃教材&#183;計算機係列:JSP網絡程序設計