簡(jiǎn)述
下面就講講我在項(xiàng)目中是如何實(shí)現(xiàn)camera視頻預(yù)覽及錄制、本地視頻獲取、視頻文件的參數(shù)校驗(yàn)以及各類(lèi)可能遇到的問(wèn)題。
相關(guān)需要了解的類(lèi)有:
android.hardware.Camera;
android.view.TextureView;
android.view.Surface;
android.graphics.SurfaceTexture;
android.media.MediaRecorder;
android.media.CamcorderProfile;
Camera相機(jī),封裝了相機(jī)的屬性和動(dòng)作绒窑,例如開(kāi)啟、預(yù)覽和停止預(yù)覽等舔亭,其中的CameraInfo定義了攝像頭的分類(lèi)些膨,前置、后置或者外設(shè)钦铺。
TextureView控件订雾,需要在你的頁(yè)面的xml文件中定義,它就是實(shí)際用戶可觀看的影像的載體矛洞。
Surface是預(yù)覽時(shí)傳入的對(duì)象洼哎。
SurfaceTexture是生成Surface對(duì)象時(shí)所必須的構(gòu)造參數(shù)。具體怎么獲取它沼本,下面將在代碼中講到噩峦。
MediaRecorder是錄制視頻的核心工具類(lèi)。
CamcorderProfile用于獲取錄制視頻的質(zhì)量抽兆。
預(yù)覽
布局文件xml
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
給TextureView設(shè)置監(jiān)聽(tīng)识补,為了得到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()
}
從字面意思就能大體知道每個(gè)方法的作用,我們的頁(yè)面沒(méi)有過(guò)多的邏輯需要辫红,所以在onSurfaceTextureAvailable方法中獲取到SurfaceTexture賦值給成員變量進(jìn)行緩存凭涂。width和height是不可靠的,我的項(xiàng)目中沒(méi)有用到贴妻。
開(kāi)啟相機(jī)切油,由于現(xiàn)在的手機(jī)基本上都存在至少兩個(gè)攝像頭,所以在開(kāi)啟之前最好設(shè)置好開(kāi)啟的是哪個(gè)揍瑟。
首先先判斷你選擇的攝像頭是否存在
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
}
然后開(kāi)啟攝像頭白翻,次方法的作用是優(yōu)先開(kāi)啟后置乍炉,若沒(méi)有后置再開(kāi)啟前置绢片,若都沒(méi)有則返回null,那么APP在手機(jī)上肯定不能用了岛琼。成員變量currentCameraFacing用于緩存當(dāng)前開(kāi)啟的是哪個(gè)攝像頭底循,切換攝像頭的時(shí)候用到。
一般情況下我們需要的是Activity onResume()的時(shí)候開(kāi)啟攝像頭槐瑞,onPause()的時(shí)候關(guān)閉攝像頭熙涤,onCreate()方法中給TextureView設(shè)置監(jiān)聽(tīng)。
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
}
開(kāi)啟攝像頭之后就是要設(shè)置預(yù)覽了
private fun Camera.preview() {
surfaceTexture?.let {
setDisplayOrientation(90) //設(shè)置預(yù)覽的旋轉(zhuǎn)角度,默認(rèn)是橫屏的祠挫,90度代表豎屏
setPreviewTexture(surfaceTexture) //設(shè)置預(yù)覽必須的控件對(duì)象
startPreview() //開(kāi)啟預(yù)覽
parameters?.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO
}
}
對(duì)焦的問(wèn)題其實(shí)很復(fù)雜那槽,可以單獨(dú)進(jìn)行專(zhuān)項(xiàng)研究的。本文是針對(duì)錄制視頻等舔,所以用它就行了Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO骚灸,前提是廠商手機(jī)支持。
注意慌植,由于surfaceTexture是設(shè)置預(yù)覽的必須參數(shù)甚牲,所以能否開(kāi)啟預(yù)覽需要兩個(gè)必要條件,1蝶柿、開(kāi)啟相機(jī)了丈钙,2、surfaceTexture被賦值了交汤。所以什么時(shí)候執(zhí)行此方法要?jiǎng)幽X筋雏赦。
fun startPreview() {
camera = openCamera()
camera?.preview()
}
fun stopPreview() {
camera?.stopPreview();
camera?.setPreviewCallback(null);
camera?.release()
camera = null
}
錄制
下面是MediaRecorder對(duì)象的具體配置方法,這里需要注意有好多坑
-camera?.unlock();setCamera(camera)芙扎,這兩行處理視頻錄制的時(shí)候會(huì)橫屏的問(wèn)題
-setVideoSize(480, 640)喉誊,這行注釋了,建議不要手動(dòng)設(shè)置尺寸纵顾,因?yàn)槊總€(gè)攝像頭硬件都有支持尺寸的一個(gè)列表伍茄,是固定的,差一個(gè)像素都會(huì)崩潰施逾,如果想要設(shè)置尺寸必須之后你的手機(jī)的屏幕像素敷矫。但是你能拿到的屏幕像素的高度外加了通知欄的高度。當(dāng)然硬是通過(guò)各種邏輯拿到和攝像頭支持的像素汉额,崩潰的風(fēng)險(xiǎn)性也很高而且邏輯更加復(fù)雜曹仗。
-setVideoFrameRate(30),默認(rèn)就可以蠕搜。
-setVideoEncodingBitRate(3 * 1024 * 1024)怎茫,既然使用了setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P)),就聽(tīng)系統(tǒng)的吧妓灌,這里也不需要額外設(shè)置了轨蛤。
-setPreviewDisplay(Surface(surfaceTexture)),必須設(shè)置虫埂,不然崩潰祥山。
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) //設(shè)置視頻的長(zhǎng)寬
// setVideoFrameRate(30) //設(shè)置視頻的幀率
// setVideoEncodingBitRate(3 * 1024 * 1024) //設(shè)置比特率(比特率越高質(zhì)量越高同樣也越大)
setOrientationHint(when (currentCameraFacing) {
Camera.CameraInfo.CAMERA_FACING_BACK -> 90
Camera.CameraInfo.CAMERA_FACING_FRONT -> 270
else -> 90
}) //這里是調(diào)整旋轉(zhuǎn)角度(前置和后置的角度不一樣)
setMaxDuration(60 * 1000) //設(shè)置記錄會(huì)話的最大持續(xù)時(shí)間(毫秒)
setPreviewDisplay(Surface(surfaceTexture)) //設(shè)置預(yù)覽對(duì)象
val folderPath = rootPath + File.separator + "uplus" //設(shè)置輸出的文件夾路徑
val folderFile = File(folderPath)
if (!folderFile.exists()) {
folderFile.mkdirs()
} //創(chuàng)建路徑,若不存在掉伏,創(chuàng)建
val fileName = "Uplus${System.currentTimeMillis()}.mp4" //設(shè)置輸出的文件名稱(chēng)
filePath = folderPath + File.separator + fileName
setOutputFile(filePath) //設(shè)置輸出文件全路徑
}
啟動(dòng)錄制
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
}
}
文件會(huì)保存起來(lái)缝呕,但是系統(tǒng)相冊(cè)還不會(huì)顯示澳窑,所以需要發(fā)一個(gè)廣播通知相冊(cè)更新你的視頻文件。
private fun updateGallery(filePath: String) {
val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
intent.data = createFileUri(filePath)
context.sendBroadcast(intent);
}
獲取本地視頻文件
獲取本地視頻文件就是獲取本地文件的絕對(duì)路徑供常,上面代碼中有個(gè)成員變量filePath摊聋,它就是自己錄制的視頻同時(shí)保存下來(lái)的絕對(duì)路徑。但是如果選擇本地相冊(cè)中的視頻文件第一時(shí)間拿到的是Uri對(duì)象栈暇,那么就需要進(jìn)行轉(zhuǎn)換栗精,代碼如下:
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
這里是比較全面的,可以獲取各種類(lèi)型的各種文件夾下的視頻文件瞻鹏。如果Uri是file開(kāi)頭的直接返回悲立,如果是content開(kāi)頭的其實(shí)是ContentProvider的存儲(chǔ)鍵值,從中拿到關(guān)鍵信息獲取filePath新博。