视频录制SurfaceView
package com.bw.videorecorder;
import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private SurfaceView surfaceView;
private RelativeLayout layout;
private Button recordbutton;
private Button stopbutton;
private MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//请求窗口的标题 为无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//要求窗口全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
//设置参数 类型 把数据推送到前方显示出来
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//设置分辨率
surfaceView.getHolder().setFixedSize(176, 144);
//要求屏幕常量不要锁屏
surfaceView.getHolder().setKeepScreenOn(true);
//找到相对布局
layout = (RelativeLayout) findViewById(R.id.layout);
recordbutton = (Button) findViewById(R.id.recordbutton);
stopbutton = (Button) findViewById(R.id.stopbutton);
}
//当用户触摸屏幕时,显示按钮
@Override
public boolean onTouchEvent(MotionEvent event) {
//当用户触摸按下时
if (event.getAction()==MotionEvent.ACTION_DOWN) {
//将相对布局设置为可见
layout.setVisibility(ViewGroup.VISIBLE);
}
return super.onTouchEvent(event);
}
public void record(View v){
switch (v.getId()) {
case R.id.recordbutton:
try {
File file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp");
mediaRecorder = new MediaRecorder();
//设置声音的来源,来源于麦克风
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置视频的来源 来源于摄像头
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//设置输出格式 3g格式 MP4
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置视频大小
mediaRecorder.setVideoSize(320, 240);
//设置捕获画面速率 5帧清晰 3帧不清晰
mediaRecorder.setVideoFrameRate(3);
//设置声音编码
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置视频编码
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//设置输出路径
mediaRecorder.setOutputFile(file.getAbsolutePath());
//把预览画面输送到屏幕上
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
//缓冲
mediaRecorder.prepare();
//开始录制
mediaRecorder.start();
//当点击刻录时,停止可以点击
recordbutton.setEnabled(false);
stopbutton.setEnabled(true);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.stopbutton:
if (mediaRecorder!=null) {
//停止
mediaRecorder.stop();
//释放
mediaRecorder.release();
mediaRecorder=null;
}
//当点击停止时,刻录可以点击
recordbutton.setEnabled(true);
stopbutton.setEnabled(false);
break;
default:
break;
}
}
}
//-------------------Xml布局-----------------------------
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceView"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout"
android:visibility="gone"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recordbutton"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dp"
android:text="@string/recordbutton"
android:onClick="record"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stopbutton"
android:layout_toLeftOf="@+id/recordbutton"
android:layout_alignTop="@+id/recordbutton"
android:layout_marginRight="30dp"
android:text="@string/stopdbutton"
android:onClick="record"/>
</RelativeLayout>
</FrameLayout>
//--------------------values 下的 string----------------------
//添加
<string name="recordbutton">刻录</string>
<string name="stopdbutton">停止</string>
//------------权限-----------------------
<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" />