在短视频火爆、5G时代的到来,不学点媒体的开发怎么说的过去呢?
本人入门Android相机开发不久,在此记录一下踩过的坑(以下总结局限于当时的个人认知以及基于当时相关版本的API)。
在Android里的相机开发里边,可能会遇到各种各样的问题,比如
FFmpeg、MediaCodec编码等
。。。。。。。。
由于问题过多,需要分多个篇幅来展开。接下来先说说Android Camera API
CameraX
https://developer.android.google.cn/training/camerax
直接封装了Preview、CameraView,可自动选择合适的预览大小,也可设置固定大小、宽高比【只能设置其中一个】
模式captureMode
摄像头切换toggleCamera()
拍照takePicture()
录像startRecording()【不能暂停恢复录制】
通过ImageCapture.Metadata()设置isReversedHorizontal = true 可将图片结果进行反转
Camera2
https://developer.android.google.cn/reference/android/hardware/camera2/package-summary
配置流程相对复杂,但网上都有许多文章
Camera1
都2020了,弃了吧
第三方库相机库
https://github.com/natario1/CameraView
文档齐全
takeVideoSnapshot 能解决前置镜像
takeVideo、takePicture 使用surface、Texture预览,并使用MediaRecorder录制,存在镜像问题
添加pause、resume功能,仅针对MediaRecorder,而takeVideoSnapshot 直接使用MediaCodec编码
https://github.com/natario1/CameraView/pull/789/commits/2d979b3f31ad392080f8d765dc2e6678dc2044f8
https://github.com/MasayukiSuda/CameraRecorder-android
使用EGLSurface进行预览,提供各种滤镜
tip:混个1024勋章,文章待完善
Android 音视频编解码
fmpeg、MediaCodec、mp4parser这几种框架的优势对比?
https://www.zhihu.com/question/278431860
FFmpeg与MediaCodec 简单总结
https://blog.csdn.net/no_367/article/details/103158862
MediaRecorder
音视频录制接口
MediaCodec:
提供对音视频压缩编码和解码功能
MediaCodec.PARAMETER_KEY_SUSPEND
设置参数使其挂起,可实现暂停和恢复录制
https://stackoverflow.com/questions/21018578/pause-resume-mediacodec
MediaMuxer
可以将音视频混合生成多媒体文件,生成MP4文件
实现暂停和恢复录制
https://developer.android.google.cn/reference/android/media/MediaCodec?hl=en
Specify an offset (in micro-second) to be added on top of the timestamps onward. A typical use case is to apply an adjust to the timestamps after a period of pause by the user. This parameter can only be used on an encoder in "surface-input" mode. The value is a long int, indicating the timestamp offset to be applied.
https://github.com/google/grafika
a dumping ground for Android graphics & media hacks
MediaRecorder.setInputSurface 不能设置其他surface
https://www.jianshu.com/p/f8570c896ca8