<?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:layout_width = "320px"
android:layout_height = "240px"
android:gravity = "center_horizontal"
/>
SurfaceView用于显示录制视频时摄像头的画面,尺寸为320*240,样式居中
<LinearLayout
android:orientation = "horizontal"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:gravity = "center_horizontal"
android:layout_marginTop = "20dp">
<Button
android:id ="@+id/recordBtn"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text ="开始录制" />
<Button
android:id ="@+id/stopBtn"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text ="停止录制" />
<Button
android:id ="@+id/deleteBtn"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text ="删除录像" />
</LinearLayout>
</LinearLayout>
package com.creative.CameraDemo;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
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.widget.Button;
public class CameraDemoActivity extends Activity
{
private File _recordVideoFile;
private MediaRecorder _vedioRecorder;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView videoView = (SurfaceView)findViewById(R.id.videoView);
final SurfaceHolder surfaceHolder = videoView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button recordBtn = (Button)findViewById(R.id.recordBtn);
Button stopBtn = (Button)findViewById(R.id.stopBtn);
Button deleteBtn = (Button)findViewById(R.id.deleteBtn);
// ��ʼ¼��
recordBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
if(_vedioRecorder == null)
{
_vedioRecorder = new MediaRecorder();
}
try
{
_recordVideoFile = File.createTempFile("video", ".3gp");//在SD卡目录下创建一个视频文件
} catch (IOException e1)
{
e1.printStackTrace();
}
_vedioRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//设置视频源为摄像头
_vedioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//设置输出格式为MPEG4
_vedioRecorder.setVideoSize(320, 240);
_vedioRecorder.setVideoFrameRate(30);///设置视频帧为每秒30帧
_vedioRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//设置编码格式为H264
_vedioRecorder.setOutputFile(_recordVideoFile.getAbsolutePath());//设置输出文件路径
_vedioRecorder.setPreviewDisplay(surfaceHolder.getSurface());//设置视频显示在SurfaceView上
try
{
_vedioRecorder.prepare();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
_vedioRecorder.start();
}});
// ֹͣ¼��
stopBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
if(_vedioRecorder != null)
{
_vedioRecorder.stop();
_vedioRecorder.release();
_vedioRecorder = null;
}
}});
// ɾ��¼��
deleteBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
_recordVideoFile.delete();
}});
}
}