Feature: add AudioEffectController

pull/209/head
xufuji456 3 years ago
parent b47bf35e47
commit bb6d6d3437
  1. 99
      AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt
  2. 19
      AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt
  3. 6
      AndroidMedia/src/main/java/com/frank/androidmedia/listener/PlayerCallback.kt
  4. 88
      app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt
  5. 2
      app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt
  6. 6
      app/src/main/java/com/frank/ffmpeg/adapter/EqualizerAdapter.kt
  7. 3
      app/src/main/res/layout/activity_audio_effect.xml

@ -0,0 +1,99 @@
package com.frank.androidmedia.controller
import android.R
import android.content.Context
import android.media.audiofx.Equalizer
import android.media.audiofx.PresetReverb
import android.util.Log
import android.util.Pair
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.SeekBar
import android.widget.Spinner
import com.frank.androidmedia.listener.AudioEffectCallback
import java.util.ArrayList
/**
* AudioEffect: EqualizerPresetReverb
*
* @author frank
* @date 2022/3/23
*/
open class AudioEffectController(audioEffectCallback: AudioEffectCallback) {
companion object {
val TAG: String = AudioEffectController::class.java.simpleName
}
private var bands: Short = 0
private var minEQLevel: Short = 0
private var mEqualizer: Equalizer? = null
private var mPresetReverb: PresetReverb? = null
private val reverbValues = ArrayList<String>()
// private var seekBarList: List<SeekBar>? = ArrayList()
private var mAudioEffectCallback: AudioEffectCallback? = null
private val presetReverb = arrayOf("None", "SmallRoom", "MediumRoom", "LargeRoom",
"MediumHall", "LargeHall", "Plate")
init {
mAudioEffectCallback = audioEffectCallback
}
fun setupEqualizer(audioSessionId: Int) {
val equalizerList = ArrayList<Pair<*, *>>()
mEqualizer = Equalizer(0, audioSessionId)
mEqualizer!!.enabled = true
// band level: min and max
minEQLevel = mEqualizer!!.bandLevelRange[0]//min level
val maxEQLevel = mEqualizer!!.bandLevelRange[1] // max level
bands = mEqualizer!!.numberOfBands
for (i in 0 until bands) {
val centerFreq = (mEqualizer!!.getCenterFreq(i.toShort()) / 1000).toString() + " Hz"
val pair = Pair.create(centerFreq, mEqualizer!!.getBandLevel(i.toShort()) - minEQLevel)
equalizerList.add(pair)
}
mAudioEffectCallback?.setEqualizerList(maxEQLevel - minEQLevel, equalizerList)
}
fun setupPresetStyle(context: Context, spinnerStyle: Spinner) {
for (i in 0 until mEqualizer!!.numberOfPresets) {
reverbValues.add(mEqualizer!!.getPresetName(i.toShort()))
}
spinnerStyle.adapter = ArrayAdapter(context, R.layout.simple_spinner_item, reverbValues)
spinnerStyle.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, arg2: Int, arg3: Long) {
try {
mEqualizer!!.usePreset(arg2.toShort())
val seekBarList: List<SeekBar>? = mAudioEffectCallback?.getSeeBarList()
if (bands > 0 && seekBarList != null && mEqualizer != null) {
for (band in 0 until bands) {
seekBarList[band].progress = mEqualizer!!.getBandLevel(band.toShort()) - minEQLevel
}
}
} catch (e: Exception) {
Log.e(TAG, "preset style error=$e")
}
}
override fun onNothingSelected(arg0: AdapterView<*>) {}
}
}
fun onEqualizerProgress(index: Int, progress: Int) {
mEqualizer!!.setBandLevel(index.toShort(), (progress + minEQLevel).toShort())
}
fun release() {
mEqualizer?.release()
mPresetReverb?.release()
}
}

@ -0,0 +1,19 @@
package com.frank.androidmedia.listener
import android.util.Pair
import android.widget.SeekBar
import java.util.ArrayList
/**
* The callback of AudioEffect
*
* @author frank
* @date 2022/3/23
*/
interface AudioEffectCallback {
fun getSeeBarList(): List<SeekBar>?
fun setEqualizerList(maxProgress: Int, equalizerList: ArrayList<Pair<*, *>>)
}

@ -1,9 +1,9 @@
package com.frank.androidmedia.listener
/**
* @author xufulong
* @date 3/18/22 2:25 PM
* @desc
*
* @author frank
* @date 2022/3/18
*/
interface PlayerCallback {

@ -13,6 +13,8 @@ import android.widget.*
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.frank.androidmedia.controller.AudioEffectController
import com.frank.androidmedia.listener.AudioEffectCallback
import com.frank.ffmpeg.R
import com.frank.ffmpeg.adapter.EqualizerAdapter
import com.frank.ffmpeg.format.AudioVisualizer
@ -23,37 +25,29 @@ import java.io.IOException
import java.util.ArrayList
/**
* Audio effect: equalizer, enhancer, visualizer
* Audio effect: equalizer, enhancer, visualizer, bassBoost
* Created by frank on 2020/10/20.
*/
class AudioEffectActivity : BaseActivity(), OnSeeBarListener {
class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallback {
companion object {
private val TAG = AudioEffectActivity::class.java.simpleName
private val audioPath = Environment.getExternalStorageDirectory().path + "/change.mp3"
private val audioPath = Environment.getExternalStorageDirectory().path + "/tiger.mp3"
}
private var mPlayer: MediaPlayer? = null
private var mEqualizer: Equalizer? = null
private var mBass: BassBoost? = null
private var mPresetReverb: PresetReverb? = null
private val reverbValues = ArrayList<String>()
private var seekBarList: List<SeekBar>? = ArrayList()
private var bands: Short = 0
private var minEQLevel: Short = 0
private var spinnerStyle: Spinner? = null
private var spinnerReverb: Spinner? = null
private var barBassBoost: SeekBar? = null
private var equalizerAdapter: EqualizerAdapter? = null
private val enableEqualizer = true
private var loudnessEnhancer: LoudnessEnhancer? = null
private var barEnhancer: SeekBar? = null
private var visualizerView: VisualizerView? = null
private var mVisualizer: AudioVisualizer? = null
private val presetReverb = arrayOf("None", "SmallRoom", "MediumRoom", "LargeRoom",
"MediumHall", "LargeHall", "Plate")
private var audioEffectController: AudioEffectController? = null
private val permissions = arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
@ -61,8 +55,9 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener {
Manifest.permission.MODIFY_AUDIO_SETTINGS)
private val onPreparedListener = MediaPlayer.OnPreparedListener {
setupEqualizer()
setupPresetStyle()
audioEffectController = AudioEffectController(this)
audioEffectController?.setupEqualizer(mPlayer!!.audioSessionId)
audioEffectController?.setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!)
setupBassBoost()
setLoudnessEnhancer()
setupVisualizer()
@ -117,60 +112,12 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener {
}
override fun onProgress(index: Int, progress: Int) {
mEqualizer!!.setBandLevel(index.toShort(), (progress + minEQLevel).toShort())
}
private fun setupEqualizer() {
val equalizerList = ArrayList<Pair<*, *>>()
mEqualizer = Equalizer(0, mPlayer!!.audioSessionId)
mEqualizer!!.enabled = enableEqualizer
// band level: min and max
minEQLevel = mEqualizer!!.bandLevelRange[0]//min level
val maxEQLevel = mEqualizer!!.bandLevelRange[1] // max level
bands = mEqualizer!!.numberOfBands
for (i in 0 until bands) {
val centerFreq = (mEqualizer!!.getCenterFreq(i.toShort()) / 1000).toString() + " Hz"
val pair = Pair.create(centerFreq, mEqualizer!!.getBandLevel(i.toShort()) - minEQLevel)
equalizerList.add(pair)
}
if (equalizerAdapter != null) {
equalizerAdapter!!.setMaxProgress(maxEQLevel - minEQLevel)
equalizerAdapter!!.setEqualizerList(equalizerList)
}
}
private fun setupPresetStyle() {
for (i in 0 until mEqualizer!!.numberOfPresets) {
reverbValues.add(mEqualizer!!.getPresetName(i.toShort()))
}
spinnerStyle!!.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, reverbValues)
spinnerStyle!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, arg2: Int, arg3: Long) {
try {
mEqualizer!!.usePreset(arg2.toShort())
if (equalizerAdapter != null) {
seekBarList = equalizerAdapter!!.getSeekBarList()
}
if (bands > 0 && seekBarList != null && mEqualizer != null) {
for (band in 0 until bands) {
seekBarList!![band].progress = mEqualizer!!.getBandLevel(band.toShort()) - minEQLevel
}
}
} catch (e: Exception) {
Log.e(TAG, "preset style error=$e")
}
}
override fun onNothingSelected(arg0: AdapterView<*>) {}
}
audioEffectController?.onEqualizerProgress(index, progress)
}
private fun setupBassBoost() {
mBass = BassBoost(0, mPlayer!!.audioSessionId)
mBass!!.enabled = enableEqualizer
mBass!!.enabled = true
// 0--1000
barBassBoost!!.max = 1000
barBassBoost!!.progress = 0
@ -234,11 +181,20 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener {
mVisualizer?.releaseVisualizer()
}
override fun setEqualizerList(maxProgress: Int, equalizerList: ArrayList<Pair<*, *>>) {
if (equalizerAdapter != null) {
equalizerAdapter!!.setMaxProgress(maxProgress)
equalizerAdapter!!.setEqualizerList(equalizerList)
}
}
override fun getSeeBarList(): List<SeekBar>? {
return equalizerAdapter?.getSeekBarList()
}
override fun onDestroy() {
super.onDestroy()
mEqualizer?.release()
mPresetReverb?.release()
mBass?.release()
loudnessEnhancer?.release()
releaseVisualizer()

@ -38,7 +38,7 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener {
private var mAudioPlayer: AudioPlayer? = null
private var visualizerView: VisualizerView? = null
private var equalizerAdapter: EqualizerAdapter? = null
private var audioPath = Environment.getExternalStorageDirectory().path + "/know_play.mp3"
private var audioPath = Environment.getExternalStorageDirectory().path + "/tiger.mp3"
override val layoutId: Int
get() = R.layout.activity_equalizer

@ -43,9 +43,7 @@ class EqualizerAdapter(private val context: Context, private val onSeeBarListene
holder.barEqualizer.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
// if (fromUser) {
// onSeeBarListener?.onProgress(i, progress)
// }
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
@ -62,7 +60,7 @@ class EqualizerAdapter(private val context: Context, private val onSeeBarListene
return if (equalizerList != null) equalizerList!!.size else 0
}
private inner class EqualizerHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
private inner class EqualizerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val txtFrequency: TextView = itemView.findViewById(R.id.txt_frequency)
val barEqualizer: SeekBar = itemView.findViewById(R.id.bar_equalizer)

@ -43,7 +43,8 @@
android:id="@+id/spinner_reverb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
android:layout_marginTop="5dp"
android:visibility="gone">
</Spinner>
<TextView

Loading…
Cancel
Save