From bb6d6d34377594c0fe967118c3a05e49645fe59c Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Wed, 23 Mar 2022 11:57:39 +0800 Subject: [PATCH] Feature: add AudioEffectController --- .../controller/AudioEffectController.kt | 99 +++++++++++++++++++ .../listener/AudioEffectCallback.kt | 19 ++++ .../androidmedia/listener/PlayerCallback.kt | 6 +- .../ffmpeg/activity/AudioEffectActivity.kt | 88 +++++------------ .../ffmpeg/activity/EqualizerActivity.kt | 2 +- .../frank/ffmpeg/adapter/EqualizerAdapter.kt | 6 +- .../main/res/layout/activity_audio_effect.xml | 3 +- 7 files changed, 148 insertions(+), 75 deletions(-) create mode 100644 AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt create mode 100644 AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt new file mode 100644 index 0000000..1c9a3a0 --- /dev/null +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt @@ -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: Equalizer、PresetReverb + * + * @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() +// private var seekBarList: List? = 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>() + 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? = 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() + } + + +} \ No newline at end of file diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt new file mode 100644 index 0000000..3aaeb58 --- /dev/null +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt @@ -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? + + fun setEqualizerList(maxProgress: Int, equalizerList: ArrayList>) + +} \ No newline at end of file diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/listener/PlayerCallback.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/listener/PlayerCallback.kt index c71de48..092c5d7 100644 --- a/AndroidMedia/src/main/java/com/frank/androidmedia/listener/PlayerCallback.kt +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/listener/PlayerCallback.kt @@ -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 { diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt index 2027eb6..be05b83 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt @@ -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() - private var seekBarList: List? = 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>() - 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>) { + if (equalizerAdapter != null) { + equalizerAdapter!!.setMaxProgress(maxProgress) + equalizerAdapter!!.setEqualizerList(equalizerList) + } + } + + override fun getSeeBarList(): List? { + return equalizerAdapter?.getSeekBarList() + } + override fun onDestroy() { super.onDestroy() - mEqualizer?.release() - mPresetReverb?.release() mBass?.release() loudnessEnhancer?.release() releaseVisualizer() diff --git a/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt index 70fb909..083a299 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt @@ -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 diff --git a/app/src/main/java/com/frank/ffmpeg/adapter/EqualizerAdapter.kt b/app/src/main/java/com/frank/ffmpeg/adapter/EqualizerAdapter.kt index 88a22e5..307504f 100644 --- a/app/src/main/java/com/frank/ffmpeg/adapter/EqualizerAdapter.kt +++ b/app/src/main/java/com/frank/ffmpeg/adapter/EqualizerAdapter.kt @@ -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) diff --git a/app/src/main/res/layout/activity_audio_effect.xml b/app/src/main/res/layout/activity_audio_effect.xml index ccd923c..b0ac724 100644 --- a/app/src/main/res/layout/activity_audio_effect.xml +++ b/app/src/main/res/layout/activity_audio_effect.xml @@ -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">