Skip to content

Commit

Permalink
增加视频质量设置,解决录像未完成销毁Activity崩溃的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
jxj2118 committed Sep 17, 2019
1 parent c1fd604 commit e2190c2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ Android 仿微信视频拍摄 支持触摸拍摄 长按拍摄,采用camera2,
<br>
<a href="art/app-debug.apk">点击下载</a>

## Version
2018-9-17 V1.0.2 增加视频质量设置,解决录像未完成销毁Activity崩溃的BUG
<br>
2018-8-13 V1.0.1 优化尺寸匹配算法,多摄像头匹配,解决华为设备崩溃
<br>
2018-5-30 V1.0.0

## Gradle
Step 1. Add the JitPack repository to your build file
```groovy
Expand All @@ -44,7 +51,7 @@ allprojects {
Step 2. Add the dependency
```groovy
dependencies {
implementation 'com.github.jxj2118:JCamera:v1.0.0'
implementation 'com.github.jxj2118:JCamera:v1.0.2'
}
```
Expand All @@ -68,6 +75,7 @@ android {
.allowPhoto(true)// 是否允许拍照 默认允许
.allowRecord(true)// 是否允许录像 默认允许
.setMaxRecordTime(3)//最长录像时间 秒
.setRecordQuality(30)//设置视频质量 1-100 默认30
.start(this,PHOTO_OR_VIDEO_FOR_CAMERA)//PHOTO_OR_VIDEO_FOR_CAMERA 请求码 回调时可用
```

Expand Down
Binary file modified art/app-debug.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion jcamera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "v1.0.0"
versionName "v1.0.2"

}

Expand Down
50 changes: 27 additions & 23 deletions jcamera/src/main/java/com/longrou/jcamera/CameraActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import android.util.Size
import android.util.SparseIntArray
import android.view.*
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
Expand All @@ -39,9 +38,7 @@ import com.longrou.jcamera.view.CircleProgressButton
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.lang.Exception
import java.lang.Long.signum
import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -314,13 +311,13 @@ class CameraActivity : AppCompatActivity() {
CameraConfig.FRONT_CAMERA_ID = cId
CameraConfig.FRONT_CAMERA_CHARACTERISTIC = cameraCharacteristics
} else if (cameraCharacteristics[CameraCharacteristics.LENS_FACING] == CameraCharacteristics.LENS_FACING_BACK) {
Log.e(TAG,"cId :$cId LENS_FACING_BACK $cameraCharacteristics")
// Log.e(TAG,"cId :$cId LENS_FACING_BACK $cameraCharacteristics")
val streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
val supportedSizes = streamConfigurationMap?.getOutputSizes(SurfaceTexture::class.java)
if (supportedSizes != null) {
for (size in supportedSizes) {
val aspectRatio = (size.width.toFloat() / size.height.toFloat())
Log.e("CameraUtil","aspectRatio $aspectRatio - width ${size.width.toFloat()} - height ${ size.height.toFloat()}")
// Log.e("CameraUtil","aspectRatio $aspectRatio - width ${size.width.toFloat()} - height ${ size.height.toFloat()}")
}
}
if (CameraConfig.BACK_CAMERA_ID.isBlank()){
Expand Down Expand Up @@ -742,25 +739,32 @@ class CameraActivity : AppCompatActivity() {
} else {
uri = Uri.fromFile(File(comRecordPath))
}
mediaPlayer.setDataSource(this@CameraActivity, uri)
//AudioAttributes是一个封装音频各种属性的类
val attrBuilder = AudioAttributes.Builder()
//************************************* 横向拍摄需要修改preview显示方向
if (recordOrientation == 90 || recordOrientation == 270){
setHorizontalPreview()
}
//*************************************
//设置音频流的合适属性
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC)
mediaPlayer.setAudioAttributes(attrBuilder.build())
mediaPlayer.setSurface(Surface(binding.mTextureView.surfaceTexture))
mediaPlayer.setOnPreparedListener {
mediaPlayer.isLooping = true
mediaPlayer.start()
//移除原视频
//还没开始预览便退出 mediaPlayer处于end状态则清除数据
try {
mediaPlayer.setDataSource(this@CameraActivity, uri)
//AudioAttributes是一个封装音频各种属性的类
val attrBuilder = AudioAttributes.Builder()
//************************************* 横向拍摄需要修改preview显示方向
if (recordOrientation == 90 || recordOrientation == 270){
setHorizontalPreview()
}
//*************************************
//设置音频流的合适属性
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC)
mediaPlayer.setAudioAttributes(attrBuilder.build())
mediaPlayer.setSurface(Surface(binding.mTextureView.surfaceTexture))
mediaPlayer.setOnPreparedListener {
mediaPlayer.isLooping = true
mediaPlayer.start()
//移除原视频
File(recordPath).delete()
}
mediaPlayer.prepare()
}catch (e: IllegalStateException){
//移除临时文件
File(comRecordPath).delete()
File(recordPath).delete()
}
mediaPlayer.prepare()
}
}).start()
}catch (e: Exception){
Expand Down Expand Up @@ -797,7 +801,7 @@ class CameraActivity : AppCompatActivity() {
setVideoSource(MediaRecorder.VideoSource.SURFACE)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setOutputFile(recordPath)
setVideoEncodingBitRate(1024 * 1024)
setVideoEncodingBitRate(1024 * 1024 * CameraConfig.RECORD_QUALITY)
setVideoFrameRate(30)
setMaxDuration(CameraConfig.MAX_RECORD_TIME * 1000)
setVideoSize(recordSize.width, recordSize.height)
Expand Down
12 changes: 12 additions & 0 deletions jcamera/src/main/java/com/longrou/jcamera/JCamera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ class JCamera {
CameraConfig.IS_ALLOW_PHOTO = boolean
return this
}

/**
* 设置视频质量 1 - 100
*/
fun setRecordQuality(quality: Int):JCamera{
if (quality in 1..100){
CameraConfig.RECORD_QUALITY = quality
}else{
CameraConfig.RECORD_QUALITY = 30
}
return this
}
fun start(activity: Activity,requestCode: Int){
val intent = Intent(activity, CameraActivity::class.java)
activity.startActivityForResult(intent, requestCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class CameraConfig {
var IS_ALLOW_RECORD : Boolean = true
//是否允许录拍照
var IS_ALLOW_PHOTO : Boolean = true
//视频质量 主要是配置帧率 范围定在 1 - 100
var RECORD_QUALITY : Int = 30

var test = 0

Expand Down

0 comments on commit e2190c2

Please sign in to comment.