1,用到的类 MediaRecorder SurfaceView SurfaceViewHolder Camera
前面的几个类在上篇实现录音功能已经介绍过了 ,就不在这里介绍了
下面就具体看代码
public class ToViewActivity extends AppCompatActivity {
private SurfaceView msurfaceView;
private MediaRecorder mrecorder;
private SurfaceHolder mholder;
private Camera mcamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_view);
//获得surfaceView的实例
msurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
initView();
}
//初始化,SurfaceViewHolder
private void initView() {
mholder = msurfaceView.getHolder();
//设置类型
mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 添加SurfaceHolder.Callback 回调接口
mholder.addCallback(new Myholder());
}
//开始录像
public void takeVideo(View view) {
try {
initRecord();
mrecorder.prepare();
mrecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
//停止录像
public void stopVideo(View view) {
mrecorder.stop();
}
private void initRecord() {
//停止预览
mcamera.stopPreview();
// 不预览了,释放掉SurfaceView
// mcamera.unlock();
mcamera.release();
mcamera = null;
//录像机
mrecorder = new MediaRecorder();
//设置预览区域
mrecorder.setPreviewDisplay(mholder.getSurface());
//设置视频来源 摄像头
mrecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//输出格式
mrecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//设置视频的编码
mrecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//录像文件路径
long time = System.currentTimeMillis();
String path = "/sdcard/aa/" + time + ".mp4";
mrecorder.setOutputFile(path);
}
class Myholder implements SurfaceHolder.Callback {
@Override// surfaceView创建的时候回调的方法
public void surfaceCreated(SurfaceHolder holder) {
try {
//打开 照相机
mcamera = Camera.open();
//设置预览
mcamera.setPreviewDisplay(holder);
//设置角度
mcamera.setDisplayOrientation(90);
//开始预览
mcamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override// surfaceView 改变的时候回调的方法
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
// 当surfaceView 销毁的时候回调的方法
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
}
xml文件的布局
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.qianfeng.doy40_photo.ToViewActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="500dp" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_start"
android:layout_width="wrap_content"
android:onClick="takeVideo"
android:layout_height="wrap_content"
android:text="开始录像" />
<Button
android:id="@+id/bt_pause"
android:onClick="stopVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止录像" />
</LinearLayout>
</LinearLayout>
清单文件 的权限
<!-- 写入内存卡的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 录像的权限-->
<uses-permission android:name="android.permission.CAMERA" />