音视频采集

第一步:在功能清单文件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:android="http://schemas.android.com/apk/res/android"
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:android="http://schemas.android.com/apk/res/android"
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:id
="@+id/surfaceview"
/>
<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:id
="@+id/record"
android:text
="@string/record"
/>
<Button
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:layout_marginLeft
="10dip"
android:id
="@+id/stop"
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();
}

}

}
}
posted @ 2011-06-08 00:30  飞鹰小谭  阅读(582)  评论(0编辑  收藏  举报