Feature: add AudioEffect of BassBoost

pull/209/head
xufuji456 3 years ago
parent bb6d6d3437
commit b241c24133
  1. 32
      AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt
  2. 23
      app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt

@ -2,6 +2,7 @@ package com.frank.androidmedia.controller
import android.R import android.R
import android.content.Context import android.content.Context
import android.media.audiofx.BassBoost
import android.media.audiofx.Equalizer import android.media.audiofx.Equalizer
import android.media.audiofx.PresetReverb import android.media.audiofx.PresetReverb
import android.util.Log import android.util.Log
@ -32,7 +33,8 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) {
private var mPresetReverb: PresetReverb? = null private var mPresetReverb: PresetReverb? = null
private val reverbValues = ArrayList<String>() private val reverbValues = ArrayList<String>()
// private var seekBarList: List<SeekBar>? = ArrayList()
private var mBass: BassBoost? = null
private var mAudioEffectCallback: AudioEffectCallback? = null private var mAudioEffectCallback: AudioEffectCallback? = null
@ -43,6 +45,9 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) {
mAudioEffectCallback = audioEffectCallback mAudioEffectCallback = audioEffectCallback
} }
/**
* Setup AudioEffect of Equalizer, which has centerFrequencybandbandLevel
*/
fun setupEqualizer(audioSessionId: Int) { fun setupEqualizer(audioSessionId: Int) {
val equalizerList = ArrayList<Pair<*, *>>() val equalizerList = ArrayList<Pair<*, *>>()
mEqualizer = Equalizer(0, audioSessionId) mEqualizer = Equalizer(0, audioSessionId)
@ -59,6 +64,9 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) {
mAudioEffectCallback?.setEqualizerList(maxEQLevel - minEQLevel, equalizerList) mAudioEffectCallback?.setEqualizerList(maxEQLevel - minEQLevel, equalizerList)
} }
/**
* Setup preset style, which associates to Equalizer
*/
fun setupPresetStyle(context: Context, spinnerStyle: Spinner) { fun setupPresetStyle(context: Context, spinnerStyle: Spinner) {
for (i in 0 until mEqualizer!!.numberOfPresets) { for (i in 0 until mEqualizer!!.numberOfPresets) {
reverbValues.add(mEqualizer!!.getPresetName(i.toShort())) reverbValues.add(mEqualizer!!.getPresetName(i.toShort()))
@ -86,11 +94,33 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) {
} }
} }
/**
* Setup AudioEffect of BassBoost
*/
fun setupBassBoost(audioSessionId: Int, barBassBoost: SeekBar) {
mBass = BassBoost(0, audioSessionId)
mBass!!.enabled = true
// 0~1000
barBassBoost.max = 1000
barBassBoost.progress = 0
barBassBoost.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
// set the strength of bass boost
mBass!!.setStrength(progress.toShort())
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
}
fun onEqualizerProgress(index: Int, progress: Int) { fun onEqualizerProgress(index: Int, progress: Int) {
mEqualizer!!.setBandLevel(index.toShort(), (progress + minEQLevel).toShort()) mEqualizer!!.setBandLevel(index.toShort(), (progress + minEQLevel).toShort())
} }
fun release() { fun release() {
mBass?.release()
mEqualizer?.release() mEqualizer?.release()
mPresetReverb?.release() mPresetReverb?.release()
} }

@ -37,7 +37,6 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac
} }
private var mPlayer: MediaPlayer? = null private var mPlayer: MediaPlayer? = null
private var mBass: BassBoost? = null
private var spinnerStyle: Spinner? = null private var spinnerStyle: Spinner? = null
private var spinnerReverb: Spinner? = null private var spinnerReverb: Spinner? = null
private var barBassBoost: SeekBar? = null private var barBassBoost: SeekBar? = null
@ -58,7 +57,7 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac
audioEffectController = AudioEffectController(this) audioEffectController = AudioEffectController(this)
audioEffectController?.setupEqualizer(mPlayer!!.audioSessionId) audioEffectController?.setupEqualizer(mPlayer!!.audioSessionId)
audioEffectController?.setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!) audioEffectController?.setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!)
setupBassBoost() audioEffectController?.setupBassBoost(mPlayer!!.audioSessionId, barBassBoost!!)
setLoudnessEnhancer() setLoudnessEnhancer()
setupVisualizer() setupVisualizer()
@ -115,24 +114,6 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac
audioEffectController?.onEqualizerProgress(index, progress) audioEffectController?.onEqualizerProgress(index, progress)
} }
private fun setupBassBoost() {
mBass = BassBoost(0, mPlayer!!.audioSessionId)
mBass!!.enabled = true
// 0--1000
barBassBoost!!.max = 1000
barBassBoost!!.progress = 0
barBassBoost!!.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
// set the strength of bass boost
mBass!!.setStrength(progress.toShort())
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
}
@RequiresApi(Build.VERSION_CODES.KITKAT) @RequiresApi(Build.VERSION_CODES.KITKAT)
private fun setLoudnessEnhancer() { private fun setLoudnessEnhancer() {
loudnessEnhancer = LoudnessEnhancer(mPlayer!!.audioSessionId) loudnessEnhancer = LoudnessEnhancer(mPlayer!!.audioSessionId)
@ -195,7 +176,7 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
mBass?.release() audioEffectController?.release()
loudnessEnhancer?.release() loudnessEnhancer?.release()
releaseVisualizer() releaseVisualizer()
mPlayer?.release() mPlayer?.release()

Loading…
Cancel
Save