Android录制微信小视频

如果做聊天开发小视频的需求,本例可作参考。

解决方案:首先,调用系统相机是完全可以实现该功能的,但是需要考虑到压缩的问题,需要转换编码格式,耗时太长,损耗太大。

其次,就是边录边转,也是一种方式,但是会导致录制过程中卡顿。

最后,使用MP4格式录制,需要使用到MediaRecord类来进行操作,设置音频源以及视频源就可以进行录制。

第一种最简单:不多说,直接上代码

file_name_video = "video.mp4";
videoFile = new File(image_path + File.separator + file_name_video);
if (!videoFile.getParentFile().exists()) {
    videoFile.getParentFile().mkdirs();
}
if (!videoFile.exists()) {
    try {
        videoFile.createNewFile();
    } catch (IOException e) {
        Toast.makeText(ChatActivity.this, "SD卡不可用,请稍后重试", Toast.LENGTH_SHORT).show();
    }
}
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
直接回调完事。

第二种比较费劲,没有写代码去研究,直接放弃了。

最后一种就是现在使用的,功能还不错,而且录制视频能够保证在10s的情况下不超过1MB,对于用户的流量使用也是极其节省的,而且清晰度的话也是能够做到能看清,不会失真太厉害。
这种自定义视频网上有很多,但是测试会发现各种问题。
当然这也是由于Android碎片化严重导致相机的制式各不相同导致的。
mMediaRecorder.setVideoFrameRate(16);如果报错,可以试试注释掉这行。
后面还会有各种各样的问题,还在研究阶段,敬请讨论。
 
 
private void initRecord() throws IOException {
    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.reset();
    if (mCamera != null)
        mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setOnErrorListener(this);
    mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
    mMediaRecorder.setVideoSource(VideoSource.CAMERA);// 视频源
    mMediaRecorder.setAudioSource(AudioSource.MIC);// 音频源
    mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);// 视频输出格式
    mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);// 音频格式
    mMediaRecorder.setVideoSize(mWidth, mHeight);// 设置分辨率:
    // mMediaRecorder.setVideoFrameRate(16);
    mMediaRecorder.setVideoEncodingBitRate(1 * 1280 * 720);// 设置帧频率,然后就清晰了
    mMediaRecorder.setOrientationHint(90);// 输出旋转90度,保持竖屏录制
    mMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);// 视频录制格式
    // mediaRecorder.setMaxDuration(Constant.MAXVEDIOTIME * 1000);
    mMediaRecorder.setOutputFile(mRecordFile.getAbsolutePath());
    mMediaRecorder.prepare();
    try {
        mMediaRecorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

ps:权限很重要。

项目github地址:点击打开链接

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值