Skip to content

Commit

Permalink
🐛 部分手机普通录音如果添加Audioeffect, 导致通路打开失败,导致崩溃。新增enableAudioEffect用于判断设置添加A…
Browse files Browse the repository at this point in the history
…udioeffect
  • Loading branch information
SheTieJun committed Sep 29, 2024
1 parent bf78c33 commit 3db083c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ abstract class BaseRecorder {

//设置背景音乐的监听
abstract fun setBackgroundMusicListener(listener: PlayerListener): BaseRecorder


//是否添加AudioEffect,普通录音,不要添加audioeffect,
fun enableAudioEffect(enable: Boolean) : BaseRecorder{/*...*/}

//初始Lame录音输出质量
abstract fun setMp3Quality(@IntRange(from = 0, to = 9) mp3Quality: Int): BaseRecorder
Expand Down
9 changes: 7 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
id("com.google.devtools.ksp")
}

androidApplication("me.shetj.mp3recorder"){
androidApplication("me.shetj.mp3recorder") {
defaultConfig {
minSdk = 24
ndk {
Expand All @@ -30,8 +30,13 @@ androidApplication("me.shetj.mp3recorder"){
signingConfig = signingConfigs.getByName("release")
}
}
packaging {
jniLibs {
useLegacyPackaging = true
}
}
sourceSets {
getByName("main"){
getByName("main") {
jniLibs.setSrcDirs(listOf("libs"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class RecordUtils(
audioChannel = 1
mp3BitRate = 128
mp3Quality = 5
enableAudioEffect = false
recordListener = this@RecordUtils
permissionListener = this@RecordUtils
pcmListener = this@RecordUtils
Expand Down
18 changes: 18 additions & 0 deletions recorder-core/src/main/java/me/shetj/recorder/core/BaseRecorder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ abstract class BaseRecorder {
private var mNoiseSuppressor: NoiseSuppressor? = null
private var mAcousticEchoCanceler: AcousticEchoCanceler? = null
private var mAutomaticGainControl: AutomaticGainControl? = null

/**
* 是否支持系统自带的去噪音,增强以及回音问题,需要自行判断
*/
private var mEnableAudioEffect:Boolean = false
//endregion 系统自带的去噪音,增强以及回音问题

/**
Expand Down Expand Up @@ -320,6 +325,15 @@ abstract class BaseRecorder {
return this
}

/**
* 是否添加AudioEffect,普通录音,不要添加audioeffect,
*
* @param enable
*/
open fun enableAudioEffect(enable: Boolean){
this.mEnableAudioEffect = enable
return
}

/**
* 设置回调
Expand Down Expand Up @@ -609,6 +623,10 @@ abstract class BaseRecorder {
* 3. 自动增益控制
*/
protected fun initAudioEffect(mAudioSessionId: Int) {
if (!mEnableAudioEffect){
releaseAEC()
return
}
if (mAudioSessionId != 0) {
if (NoiseSuppressor.isAvailable()) {
//噪声抑制
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ data class Mp3Option(
var recordListener: RecordListener? = null,
// pcm 回调
var pcmListener: PCMListener? = null,
// 是否添加AudioEffect,普通录音,不要添加audioeffect,
var enableAudioEffect:Boolean = false
)

fun recorder(block: Mp3Option.() -> Unit): Mp3Option {
Expand Down
21 changes: 20 additions & 1 deletion recorder-sim/src/main/java/me/shetj/player/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.os.Message
import android.text.TextUtils
import java.util.concurrent.atomic.AtomicBoolean


/**
* **@author:** shetj<br></br>
* **@createTime:** 2018/10/23 0023<br></br>
Expand Down Expand Up @@ -47,6 +48,7 @@ class AudioPlayer :
private var mediaPlayer: MediaPlayer? = null
private var mAudioManager: AudioManager? = null
private var context: Context? = null
private var speed = 1.0f

private var focusChangeListener =
OnAudioFocusChangeListener { focusChange ->
Expand Down Expand Up @@ -114,7 +116,7 @@ class AudioPlayer :
!hasMessages(HANDLER_PLAYING)
) {
listener!!.onProgress(mediaPlayer!!.currentPosition, mediaPlayer!!.duration)
this.sendEmptyMessageDelayed(HANDLER_PLAYING, 300)
this.sendEmptyMessageDelayed(HANDLER_PLAYING, 500)
}
HANDLER_START -> {
if (listener != null && mediaPlayer != null) {
Expand Down Expand Up @@ -366,6 +368,10 @@ class AudioPlayer :
}
}

fun getCurSpeed(): Float {
return speed
}

/**
* 恢复,并且开始计时
*/
Expand Down Expand Up @@ -421,6 +427,18 @@ class AudioPlayer :
}
}

private fun setPlaySpeed(speed: Float): Boolean {
this.speed = speed
if (mediaPlayer == null) return false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val params = mediaPlayer!!.playbackParams
params.setSpeed(speed)
mediaPlayer!!.playbackParams = params
return true
}
return false
}

/**
* 开始计时
* 使用场景:拖动结束
Expand Down Expand Up @@ -451,6 +469,7 @@ class AudioPlayer :
mediaPlayer!!.setOnCompletionListener(this)
mediaPlayer!!.setOnSeekCompleteListener(this)
mediaPlayer!!.isLooping = isLoop
setPlaySpeed(speed)
} catch (e: Exception) {
e.printStackTrace()
}
Expand Down

0 comments on commit 3db083c

Please sign in to comment.