使用MediaRecorder实现录像的功能

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" />



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值