Android录制视频---LandscapeVideoCamera的使用详情及修改

来源:互联网 发布:苹果软件更新 编辑:程序博客网 时间:2024/05/16 08:35

LandscapeVideoCamera是一个非常强大的android 视频录制库,可以选择视频尺寸以及视频质量,只允许横屏录制。对于LandscapeVideoCamera的好处就不多介绍了。网上已经有很多人介绍了


个人最忍受不了一个点是:强制用户横屏,竖屏的时候是不让录制的,这一点真的有点坑,录制过程中不小心斜过来了呢?它就停了!它就停了!它就停了!(个人体验,不否认LandscapeVideoCamera本身的强大~~~)

 ......

因项目需求原因对其进行了如下修改:

1:取消了竖屏时停止录制的动作。

2:录制结束后增加了重录功能。

3:增加了倒计时开始录制功能。

4:增加了录制过程中开启灯光的功能。

5:需求原因改变了其UI。


效果图↓


      

使用详情↓

调用类:

import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.jmolsmobile.landscapevideocapture.VideoCaptureActivity;import com.jmolsmobile.landscapevideocapture.configuration.CaptureConfiguration;import java.io.File;import java.io.FileInputStream;import java.text.DecimalFormat;import static com.jmolsmobile.landscapevideocapture.configuration.PredefinedCaptureConfigurations.CaptureQuality.HIGH;import static com.jmolsmobile.landscapevideocapture.configuration.PredefinedCaptureConfigurations.CaptureResolution.RES_1440P;/** *  @date 2017/5/8 *  @authro lipeng */public class MainActivity extends AppCompatActivity {    private Button test;    private CaptureConfiguration configuration;    public static final String EXTRA_OUTPUT_FILENAME = "com.jmolsmobile.extraoutputfilename";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        test = (Button) findViewById(R.id.test);        initLandScapeVideoCapture();        test.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                intentLandScapeVideoCapture();            }        });    }    //初始化CaptureConfiguration    private void initLandScapeVideoCapture(){        configuration = new CaptureConfiguration(RES_1440P, HIGH,60,1024,true,true);    }    private void  intentLandScapeVideoCapture(){        final Intent intent = new Intent(MainActivity.this, VideoCaptureActivity.class);        intent.putExtra(VideoCaptureActivity.EXTRA_CAPTURE_CONFIGURATION,configuration);        intent.putExtra(VideoCaptureActivity.EXTRA_OUTPUT_FILENAME, "文件名");        startActivityForResult(intent, result);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if(resultCode==RESULT_OK){            Log.e("hlog","视频路径:"+data.getStringExtra(EXTRA_OUTPUT_FILENAME));            Log.e("hlog","视频大小:"+getVideoSize(data.getStringExtra(EXTRA_OUTPUT_FILENAME)+"M"));            double size=getVideoSize(data.getStringExtra(EXTRA_OUTPUT_FILENAME));            Toast.makeText(MainActivity.this,                    "视频大小:"+size+"M",                            Toast.LENGTH_SHORT).show();        }else if(resultCode==9999){            final Intent intent = new Intent(MainActivity.this, VideoCaptureActivity.class);            intent.putExtra(VideoCaptureActivity.EXTRA_CAPTURE_CONFIGURATION,configuration);            intent.putExtra(VideoCaptureActivity.EXTRA_OUTPUT_FILENAME, "DFA2R34FAS12");            startActivityForResult(intent, 100);        }    }    private double getVideoSize(String videoPath){        File file = new File(videoPath);        long fileLength = 0;        try {            fileLength = getFileSize(file);        } catch (Exception e) {            e.printStackTrace();        }        DecimalFormat df = new DecimalFormat("#.00");        double fileSize = Double.valueOf(df.format((double)fileLength/1048576));        return fileSize;    }    /**     * 获取指定文件大小     *     * @return     * @throws Exception     */    private static long getFileSize(File file) throws Exception {        long size = 0;        if (file.exists()) {            FileInputStream fis = null;            fis = new FileInputStream(file);            size = fis.available();        } else {            file.createNewFile();            Log.e("获取文件大小", "文件不存在!");        }        return size;    }}

CaptureConfiguration对象有多种构造方式如下:

private CaptureConfiguration() {        // Default configuration    }    @Deprecated    public CaptureConfiguration(CaptureResolution resolution, CaptureQuality quality) {        videoWidth = resolution.width;        videoHeight = resolution.height;        bitrate = resolution.getBitrate(quality);    }    @Deprecated    public CaptureConfiguration(CaptureResolution resolution, CaptureQuality quality, int maxDurationSecs,                                int maxFilesizeMb, boolean showTimer) {        this(resolution, quality, maxDurationSecs, maxFilesizeMb, showTimer, false);        this.showTimer = showTimer;    }    @Deprecated    public CaptureConfiguration(CaptureResolution resolution, CaptureQuality quality, int maxDurationSecs,                                int maxFilesizeMb, boolean showTimer, boolean allowFrontFacingCamera) {        this(resolution, quality, maxDurationSecs, maxFilesizeMb);        this.showTimer = showTimer;        this.allowFrontFacingCamera = allowFrontFacingCamera;    }    @Deprecated    public CaptureConfiguration(CaptureResolution resolution, CaptureQuality quality, int maxDurationSecs,                                int maxFilesizeMb, boolean showTimer, boolean allowFrontFacingCamera,                                int videoFPS) {        this(resolution, quality, maxDurationSecs, maxFilesizeMb, showTimer, allowFrontFacingCamera);        videoFramerate = videoFPS;    }    @Deprecated    public CaptureConfiguration(CaptureResolution resolution, CaptureQuality quality, int maxDurationSecs,                                int maxFilesizeMb) {        this(resolution, quality);        maxDurationMs = maxDurationSecs * MSEC_TO_SEC;        maxFilesizeBytes = maxFilesizeMb * MBYTE_TO_BYTE;    }    @Deprecated    public CaptureConfiguration(int videoWidth, int videoHeight, int bitrate) {        this.videoWidth = videoWidth;        this.videoHeight = videoHeight;        this.bitrate = bitrate;    }    @Deprecated    public CaptureConfiguration(int videoWidth, int videoHeight, int bitrate, int maxDurationSecs, int maxFilesizeMb) {        this(videoWidth, videoHeight, bitrate);        maxDurationMs = maxDurationSecs * MSEC_TO_SEC;        maxFilesizeBytes = maxFilesizeMb * MBYTE_TO_BYTE;    }

本文中使用的是:

@Deprecated    public CaptureConfiguration(CaptureResolution resolution, CaptureQuality quality, int maxDurationSecs,                                int maxFilesizeMb, boolean showTimer, boolean allowFrontFacingCamera) {        this(resolution, quality, maxDurationSecs, maxFilesizeMb);        this.showTimer = showTimer;        this.allowFrontFacingCamera = allowFrontFacingCamera;    }


resolution对应值有:

RES_360P(WIDTH_360P, HEIGHT_360P, BITRATE_HQ_360P, BITRATE_MQ_360P, BITRATE_LQ_360P),            // LDRES_480P(WIDTH_480P, HEIGHT_480P, BITRATE_HQ_480P, BITRATE_MQ_480P, BITRATE_LQ_480P),            // SDRES_720P(WIDTH_720P, HEIGHT_720P, BITRATE_HQ_720P, BITRATE_MQ_720P, BITRATE_LQ_720P),        // HD readyRES_1080P(WIDTH_1080P, HEIGHT_1080P, BITRATE_HQ_1080P, BITRATE_MQ_1080P, BITRATE_LQ_1080P),    // HDRES_1440P(WIDTH_1440P, HEIGHT_1440P, BITRATE_HQ_1440P, BITRATE_MQ_1440P, BITRATE_LQ_1440P),    // 2KRES_2160P(WIDTH_2160P, HEIGHT_2160P, BITRATE_HQ_2160P, BITRATE_MQ_2160P, BITRATE_LQ_2160P);    // 4K

quality对应值有:

public enum CaptureQuality {        LOW, MEDIUM, HIGH;    }

maxDurationSecs为录制的最长时间,单位为秒。maxFilesizeMb为录制的最大大小,单位为M。showTimer为是否显示录制计时器。allowFrontFacingCamera为是否允许前置摄像头切换。


  

LandscapeVideoCamera源码地址:https://github.com/JeroenMols/LandscapeVideoCamera

本文demo下载:点击打开链接

希望对大家有帮助~!

  



2 0