美文网首页
MediaRecord录像MediaPlayer播放

MediaRecord录像MediaPlayer播放

作者: m_博客 | 来源:发表于2021-06-10 14:41 被阅读0次

    Android自带的MediaRecord录像以及使用MediaPlayer进行播放
    第一步:权限
    在AndroidManifest中添加权限

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    

    当然了还需要动态申请权限【在MainActivity或者开始录像的初始化界面即可】

    requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.CAMERA},1000);

    第二步:开始录制

    /**
    * 开始录制
    */

    private void startMediaRecord(){
        //设置摄像头角度 使拍摄时方向为竖直
        camera = Camera.open();
        camera.setDisplayOrientation(90);
        camera.unlock();
        mediaRecorder = new MediaRecorder();
        //设置摄像头
        mediaRecorder.setCamera(camera);
        //设置音频源 麦克风
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //设置视频源
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        //指定视频文件格式MP4
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        //设置音频编码
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        //设置视频编码
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        //设置输出角度 使本地录制的视频方向竖直
        mediaRecorder.setOrientationHint(90);
        //设置视频输出文件
        mediaRecorder.setOutputFile(new File(getExternalFilesDir(""),"a.mp4").getAbsolutePath());
        //设置视频大小
        mediaRecorder.setVideoSize(640,480);
        //设置摄像头预览画布
        mediaRecorder.setPreviewDisplay(new Surface(mTextureView.getSurfaceTexture()));
        //
        try {
            mediaRecorder.prepare();
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaRecorder.start();
    }
    

    第三步:结束录制
    /**
    * 结束录制
    */

    private void stopMediaRecord(){
        mediaRecorder.stop();
        mediaRecorder.release();
        camera.stopPreview();
        camera.release();
    }
    

    界面布局:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="前往录屏"
        android:background="@color/blue"
        android:textColor="@color/white"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:layout_marginBottom="150dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/tv_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放录屏"
        android:background="@color/blue"
        android:textColor="@color/white"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    </androidx.constraintlayout.widget.ConstraintLayout>

    界面预览:


    image.png
    image.png
    image.png

    代码地址:
    https://gitee.com/.boke.com/media-record

    写在结尾:
    目前只是简单的实现了 录屏和播放 关于视频的清晰度,播放的进度 等等会再后面记录中一一添加

    相关文章

      网友评论

          本文标题:MediaRecord录像MediaPlayer播放

          本文链接:https://www.haomeiwen.com/subject/flgueltx.html