Android 视频刻录器

音视频采集

第一步:在功能清单文件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(); //刻录完成一定要释放资源

示例代码:

package cn.itcast.videorecoder;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Camera;
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;

public class DemoActivity extends Activity implements OnClickListener {
	private Button bt_start, bt_stop;
	private SurfaceView sv;
	private SurfaceHolder holder;
	MediaRecorder recorder;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 隐藏掉activity的标题栏
		// 必须写到setcontentview之前
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 实现窗体的全屏显示
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main);
		bt_start = (Button) this.findViewById(R.id.bt_start);
		bt_stop = (Button) this.findViewById(R.id.bt_stop);
		bt_start.setOnClickListener(this);
		bt_stop.setOnClickListener(this);
		sv = (SurfaceView) this.findViewById(R.id.sv);
		holder = sv.getHolder();
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		holder.addCallback(new MySurfaceCallback());

	}

	private class MySurfaceCallback implements SurfaceHolder.Callback {
		public void surfaceCreated(SurfaceHolder holder) {
			System.out.println("surface oncreate");
			DemoActivity.this.holder = holder;
		}

		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		public void surfaceDestroyed(SurfaceHolder holder) {
			System.out.println("surface destroy");
		}

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_start:
			bt_start.setEnabled(false);
			bt_stop.setEnabled(true);

			recorder = new MediaRecorder();

			recorder.reset();
			recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			// 从照相机采集视频
			recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			recorder.setVideoSize(720, 480);
			recorder.setVideoFrameRate(3); // 每秒3帧
			recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
			// 设置视频编码方式
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			recorder.setOutputFile("/mnt/sdcard/itcast.3gp");
			recorder.setPreviewDisplay(holder.getSurface());
			try {
				recorder.prepare();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			// 预期准备
			recorder.start();// 开始刻录
			break;

		case R.id.bt_stop:
			bt_start.setEnabled(true);
			bt_stop.setEnabled(false);
			if (recorder != null) {
				recorder.stop();
				recorder.release();
				recorder = null;
			}
			break;
		}
	}
}
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/sv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="100" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0" >

        <Button
            android:id="@+id/bt_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="开始录制" />

        <Button
            android:id="@+id/bt_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/bt_start"
            android:enabled="false"
            android:text="停止录制" />
    </RelativeLayout>

</LinearLayout>

你可能感兴趣的:(Android 视频刻录器)