简述
下面就讲讲我在项目中是如何实现camera视频预览及录制、本地视频获取、视频文件的参数校验以及各类可能遇到的问题。
相关需要了解的类有:
android.hardware.Camera;
android.view.TextureView;
android.view.Surface;
android.graphics.SurfaceTexture;
android.media.MediaRecorder;
android.media.CamcorderProfile;
Camera相机,封装了相机的属性和动作,例如开启、预览和停止预览等,其中的CameraInfo定义了摄像头的分类,前置、后置或者外设。
TextureView控件,需要在你的页面的xml文件中定义,它就是实际用户可观看的影像的载体。
Surface是预览时传入的对象。
SurfaceTexture是生成Surface对象时所必须的构造参数。具体怎么获取它,下面将在代码中讲到。
MediaRecorder是录制视频的核心工具类。
CamcorderProfile用于获取录制视频的质量。
预览
布局文件xml
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
给TextureView设置监听,为了得到SurfaceTexture
textureView.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
override fun onSurfaceTextureSizeChanged(st: SurfaceTexture?, w: Int, h: Int) = Unit
override fun onSurfaceTextureUpdated(st: SurfaceTexture?) = Unit
override fun onSurfaceTextureDestroyed(st: SurfaceTexture?): Boolean = false
override fun onSurfaceTextureAvailable(st: SurfaceTexture?, w: Int, h: Int) {
surfaceTexture = st
camera?.preview()
}
从字面意思就能大体知道每个方法的作用,我们的页面没有过多的逻辑需要,所以在onSurfaceTextureAvailable方法中获取到SurfaceTexture赋值给成员变量进行缓存。width和height是不可靠的,我的项目中没有用到。
开启相机,由于现在的手机基本上都存在至少两个摄像头,所以在开启之前最好设置好开启的是哪个。
首先先判断你选择的摄像头是否存在
Camera.CameraInfo.CAMERA_FACING_FRONT //前置摄像头
Camera.CameraInfo.CAMERA_FACING_BACK //后置摄像头
private fun checkCameraFacing(facing: Int): Boolean {
val count = Camera.getNumberOfCameras()
val cameraInfo = Camera.CameraInfo()
if (count == 0) {
return false
}
for (i in 0 until count) {
Camera.getCameraInfo(i, cameraInfo)
if (facing == cameraInfo.facing) {
return true;
}
}
return false
}
然后开启摄像头,次方法的作用是优先开启后置,若没有后置再开启前置,若都没有则返回null,那么APP在手机上肯定不能用了。成员变量currentCameraFacing用于缓存当前开启的是哪个摄像头,切换摄像头的时候用到。
一般情况下我们需要的是Activity onResume()的时候开启摄像头,onPause()的时候关闭摄像头,onCreate()方法中给TextureView设置监听。
private fun openCamera(): Camera? {
var camera: Camera? = null
if (checkCameraFacing(Camera.CameraInfo.CAMERA_FACING_BACK)) {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK)
currentCameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK
} else if (checkCameraFacing(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT)
currentCameraFacing = Camera.CameraInfo.CAMERA_FACING_FRONT
}
return camera
}
开启摄像头之后就是要设置预览了
private fun Camera.preview() {
surfaceTexture?.let {
setDisplayOrientation(90) //设置预览的旋转角度,默认是横屏的,90度代表竖屏
setPreviewTexture(surfaceTexture) //设置预览必须的控件对象
startPreview() //开启预览
parameters?.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO
}
}
对焦的问题其实很复杂,可以单独进行专项研究的。本文是针对录制视频,所以用它就行了Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,前提是厂商手机支持。
注意,由于surfaceTexture是设置预览的必须参数,所以能否开启预览需要两个必要条件,1、开启相机了,2、surfaceTexture被赋值了。所以什么时候执行此方法要动脑筋。
fun startPreview() {
camera = openCamera()
camera?.preview()
}
fun stopPreview() {
camera?.stopPreview();
camera?.setPreviewCallback(null);
camera?.release()
camera = null
}
录制
下面是MediaRecorder对象的具体配置方法,这里需要注意有好多坑
-camera?.unlock();setCamera(camera),这两行处理视频录制的时候会横屏的问题
-setVideoSize(480, 640),这行注释了,建议不要手动设置尺寸,因为每个摄像头硬件都有支持尺寸的一个列表,是固定的,差一个像素都会崩溃,如果想要设置尺寸必须之后你的手机的屏幕像素。但是你能拿到的屏幕像素的高度外加了通知栏的高度。当然硬是通过各种逻辑拿到和摄像头支持的像素,崩溃的风险性也很高而且逻辑更加复杂。
-setVideoFrameRate(30),默认就可以。
-setVideoEncodingBitRate(3 * 1024 * 1024),既然使用了setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P)),就听系统的吧,这里也不需要额外设置了。
-setPreviewDisplay(Surface(surfaceTexture)),必须设置,不然崩溃。
private fun MediaRecorder.configMediaRecorder() {
camera?.unlock();
setCamera(camera)
setAudioSource(MediaRecorder.AudioSource.CAMCORDER)//声音源
setVideoSource(MediaRecorder.VideoSource.CAMERA)//视频源
if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_1080P)) {
setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P))
} else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)) {
setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P))
} else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)) {
setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P))
}
setAudioChannels(1)
// setVideoSize(480, 640) //设置视频的长宽
// setVideoFrameRate(30) //设置视频的帧率
// setVideoEncodingBitRate(3 * 1024 * 1024) //设置比特率(比特率越高质量越高同样也越大)
setOrientationHint(when (currentCameraFacing) {
Camera.CameraInfo.CAMERA_FACING_BACK -> 90
Camera.CameraInfo.CAMERA_FACING_FRONT -> 270
else -> 90
}) //这里是调整旋转角度(前置和后置的角度不一样)
setMaxDuration(60 * 1000) //设置记录会话的最大持续时间(毫秒)
setPreviewDisplay(Surface(surfaceTexture)) //设置预览对象
val folderPath = rootPath + File.separator + "uplus" //设置输出的文件夹路径
val folderFile = File(folderPath)
if (!folderFile.exists()) {
folderFile.mkdirs()
} //创建路径,若不存在,创建
val fileName = "Uplus${System.currentTimeMillis()}.mp4" //设置输出的文件名称
filePath = folderPath + File.separator + fileName
setOutputFile(filePath) //设置输出文件全路径
}
启动录制
fun startRecord() {
mediaRecorder = MediaRecorder()
mediaRecorder?.configMediaRecorder()
try {
mediaRecorder?.prepare()
mediaRecorder?.start()
} catch (e: IOException) {
e.printStackTrace()
}
}
停止录制
fun stopRecord(uriCallback: (uri: Uri?) -> Unit) {
try {
mediaRecorder?.stop()
} catch (e: IllegalStateException) {
e.printStackTrace()
} finally {
mediaRecorder?.release()
mediaRecorder = null
if (filePath != null) {
updateGallery(filePath!!)
uriCallback.invoke(createFileUri(filePath!!))
} else {
uriCallback.invoke(null)
}
filePath = null
}
}
文件会保存起来,但是系统相册还不会显示,所以需要发一个广播通知相册更新你的视频文件。
private fun updateGallery(filePath: String) {
val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
intent.data = createFileUri(filePath)
context.sendBroadcast(intent);
}
获取本地视频文件
获取本地视频文件就是获取本地文件的绝对路径,上面代码中有个成员变量filePath,它就是自己录制的视频同时保存下来的绝对路径。但是如果选择本地相册中的视频文件第一时间拿到的是Uri对象,那么就需要进行转换,代码如下:
fun getPath(context: Context, uri: Uri?): String? {
Log.logger().debug("FileHelper Uri=$uri")
var filePath: String? = null
if (uri == null) {
return filePath
}
if (ContentResolver.SCHEME_FILE == uri.scheme) {
filePath = uri.path
} else if (ContentResolver.SCHEME_CONTENT == uri.scheme) {
if (!DocumentsContract.isDocumentUri(context, uri)) {
val projection = arrayOf(MediaStore.Images.Media.DATA)
val cursor = context.contentResolver.query(uri, projection, null, null, null)
val columnIndex = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor?.moveToFirst()
columnIndex?.let {
filePath = cursor.getString(it)
}
} else if (isExternalStorageDocument(uri)) {
val documentId = DocumentsContract.getDocumentId(uri)
val split = documentId.split(":").toTypedArray()
val type = split[0]
if ("primary" == type) {
filePath = Environment.getExternalStorageDirectory().toString() + "/" + split[1]
}
} else if (isDownloadsDocument(uri)) {
val documentId = DocumentsContract.getDocumentId(uri)
val contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
java.lang.Long.valueOf(documentId))
filePath = getDataColumn(context, contentUri, null, null)
} else if (isMediaDocument(uri)) {
val documentId = DocumentsContract.getDocumentId(uri)
val split = documentId.split(":").toTypedArray()
val contentUri = when (split[0]) {
"image" -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI
"video" -> MediaStore.Video.Media.EXTERNAL_CONTENT_URI
"audio" -> MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
else -> null
}
val selection = "_id=?"
val selectionArgs = arrayOf(split[1])
contentUri?.let {
filePath = getDataColumn(context, it, selection, selectionArgs)
}
}
}
return filePath
}
private fun getDataColumn(
context: Context,
uri: Uri,
selection: String?,
selectionArgs: Array<String>?): String? {
var cursor: Cursor? = null
val column = "_data"
val projection = arrayOf(column)
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.let {
cursor.moveToFirst()
val columnIndex: Int = cursor.getColumnIndexOrThrow(column)
return cursor.getString(columnIndex)
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
cursor?.close()
}
return null
}
private fun isExternalStorageDocument(uri: Uri): Boolean = "com.android.externalstorage.documents" == uri.authority
private fun isDownloadsDocument(uri: Uri): Boolean = "com.android.providers.downloads.documents" == uri.authority
private fun isMediaDocument(uri: Uri): Boolean = "com.android.providers.media.documents" == uri.authority
这里是比较全面的,可以获取各种类型的各种文件夹下的视频文件。如果Uri是file开头的直接返回,如果是content开头的其实是ContentProvider的存储键值,从中拿到关键信息获取filePath。