From c139d9fa4e2abea482b54ea0f0e0e61c86ac0fd6 Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Wed, 23 Mar 2022 15:10:43 +0800 Subject: [PATCH] Feature: add AudioEffect of Visualizer --- .../controller/AudioEffectController.kt | 29 +++++++++++++++---- .../listener/AudioEffectCallback.kt | 2 ++ .../androidmedia/wrap}/AudioVisualizer.java | 2 +- .../ffmpeg/activity/AudioEffectActivity.kt | 29 +++++-------------- .../com/frank/ffmpeg/view/VisualizerView.kt | 22 +++++--------- 5 files changed, 41 insertions(+), 43 deletions(-) rename {app/src/main/java/com/frank/ffmpeg/format => AndroidMedia/src/main/java/com/frank/androidmedia/wrap}/AudioVisualizer.java (97%) diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt index f72ebc1..2e80047 100644 --- a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt @@ -2,10 +2,7 @@ package com.frank.androidmedia.controller import android.R import android.content.Context -import android.media.audiofx.BassBoost -import android.media.audiofx.Equalizer -import android.media.audiofx.LoudnessEnhancer -import android.media.audiofx.PresetReverb +import android.media.audiofx.* import android.os.Build import android.util.Log import android.util.Pair @@ -16,10 +13,11 @@ import android.widget.SeekBar import android.widget.Spinner import androidx.annotation.RequiresApi import com.frank.androidmedia.listener.AudioEffectCallback +import com.frank.androidmedia.wrap.AudioVisualizer import java.util.ArrayList /** - * AudioEffect: Equalizer、PresetReverb + * AudioEffect: Equalizer、PresetReverb、BassBoost、Visualizer、LoudnessEnhancer * * @author frank * @date 2022/3/23 @@ -36,6 +34,7 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { private var mBass: BassBoost? = null private var mPresetReverb: PresetReverb? = null + private var mVisualizer: AudioVisualizer? = null private var mLoudnessEnhancer: LoudnessEnhancer? = null private var mAudioEffectCallback: AudioEffectCallback? = null @@ -121,7 +120,7 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { } /** - * Setup AudioEffect of LoudnessEnhancer, which use + * Setup AudioEffect of LoudnessEnhancer, which uses to enhance loudness */ @RequiresApi(Build.VERSION_CODES.KITKAT) fun setLoudnessEnhancer(audioSessionId: Int, barEnhancer: SeekBar) { @@ -145,6 +144,23 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { }) } + /** + * Setup AudioEffect of Visualizer, which uses to show the spectrum of audio + */ + fun setupVisualizer(audioSessionId: Int) { + mVisualizer = AudioVisualizer() + mVisualizer?.initVisualizer(audioSessionId, false, true, object : Visualizer.OnDataCaptureListener { + override fun onFftDataCapture(visualizer: Visualizer?, fft: ByteArray?, samplingRate: Int) { + if (fft != null) { + mAudioEffectCallback?.onFFTDataCallback(fft) + } + } + + override fun onWaveFormDataCapture(visualizer: Visualizer?, waveform: ByteArray?, samplingRate: Int) { + } + }) + } + fun onEqualizerProgress(index: Int, progress: Int) { mEqualizer!!.setBandLevel(index.toShort(), (progress + minEQLevel).toShort()) } @@ -154,6 +170,7 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { mEqualizer?.release() mPresetReverb?.release() mLoudnessEnhancer?.release() + mVisualizer?.releaseVisualizer() } diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt index 3aaeb58..1b61d08 100644 --- a/AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt @@ -16,4 +16,6 @@ interface AudioEffectCallback { fun setEqualizerList(maxProgress: Int, equalizerList: ArrayList>) + fun onFFTDataCallback(fft: ByteArray?) + } \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/format/AudioVisualizer.java b/AndroidMedia/src/main/java/com/frank/androidmedia/wrap/AudioVisualizer.java similarity index 97% rename from app/src/main/java/com/frank/ffmpeg/format/AudioVisualizer.java rename to AndroidMedia/src/main/java/com/frank/androidmedia/wrap/AudioVisualizer.java index 3bc1d0b..dc32d83 100644 --- a/app/src/main/java/com/frank/ffmpeg/format/AudioVisualizer.java +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/wrap/AudioVisualizer.java @@ -1,4 +1,4 @@ -package com.frank.ffmpeg.format; +package com.frank.androidmedia.wrap; import android.media.audiofx.Visualizer; import android.util.Log; 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 99c8a23..4fdccbd 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt @@ -16,7 +16,6 @@ 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 import com.frank.ffmpeg.listener.OnSeeBarListener import com.frank.ffmpeg.util.FileUtil import com.frank.ffmpeg.view.VisualizerView @@ -42,7 +41,6 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac private var equalizerAdapter: EqualizerAdapter? = null private var barEnhancer: SeekBar? = null private var visualizerView: VisualizerView? = null - private var mVisualizer: AudioVisualizer? = null private var mAudioEffectController: AudioEffectController? = null @@ -57,7 +55,7 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac mAudioEffectController?.setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!) mAudioEffectController?.setupBassBoost(mPlayer!!.audioSessionId, barBassBoost!!) mAudioEffectController?.setLoudnessEnhancer(mPlayer!!.audioSessionId, barEnhancer!!) - setupVisualizer() + mAudioEffectController?.setupVisualizer(mPlayer!!.audioSessionId) mPlayer!!.start() } @@ -112,20 +110,6 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac mAudioEffectController?.onEqualizerProgress(index, progress) } - private fun setupVisualizer() { - mVisualizer = AudioVisualizer() - mVisualizer?.initVisualizer(mPlayer!!.audioSessionId, false, true, object : Visualizer.OnDataCaptureListener { - override fun onFftDataCapture(visualizer: Visualizer?, fft: ByteArray?, samplingRate: Int) { - if (visualizerView != null && fft != null) { - visualizerView!!.post { visualizerView!!.setWaveData(fft) } - } - } - - override fun onWaveFormDataCapture(visualizer: Visualizer?, waveform: ByteArray?, samplingRate: Int) { - } - }) - } - override fun onViewClick(view: View) { } @@ -134,10 +118,6 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac } - private fun releaseVisualizer() { - mVisualizer?.releaseVisualizer() - } - override fun setEqualizerList(maxProgress: Int, equalizerList: ArrayList>) { if (equalizerAdapter != null) { equalizerAdapter!!.setMaxProgress(maxProgress) @@ -149,11 +129,16 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac return equalizerAdapter?.getSeekBarList() } + override fun onFFTDataCallback(fft: ByteArray?) { + if (fft != null && visualizerView != null) { + visualizerView!!.post { visualizerView!!.setWaveData(fft) } + } + } + override fun onDestroy() { super.onDestroy() mAudioEffectController?.release() - releaseVisualizer() mPlayer?.release() } diff --git a/app/src/main/java/com/frank/ffmpeg/view/VisualizerView.kt b/app/src/main/java/com/frank/ffmpeg/view/VisualizerView.kt index 19dfb1f..b7c366e 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/VisualizerView.kt +++ b/app/src/main/java/com/frank/ffmpeg/view/VisualizerView.kt @@ -17,14 +17,11 @@ import java.util.ArrayList */ class VisualizerView : View { - private var upShowStyle = ShowStyle.STYLE_HOLLOW_LUMP - + private var wavePath = Path() + private var lumpPaint: Paint? = null private var waveData: ByteArray? = null private var pointList: MutableList? = null - - private var lumpPaint: Paint? = null - private var wavePath = Path() - + private var mShowStyle = ShowStyle.STYLE_HOLLOW_LUMP constructor(context: Context) : super(context) { init() @@ -53,9 +50,8 @@ class VisualizerView : View { invalidate() } - - fun setStyle(upShowStyle: ShowStyle) { - this.upShowStyle = upShowStyle + fun setStyle(showStyle: ShowStyle) { + this.mShowStyle = showStyle } override fun onDraw(canvas: Canvas) { @@ -72,11 +68,9 @@ class VisualizerView : View { continue } - when (upShowStyle) { + when (mShowStyle) { ShowStyle.STYLE_HOLLOW_LUMP -> drawLump(canvas, i, false) ShowStyle.STYLE_WAVE -> drawWave(canvas, i, false) - else -> { - } } } } @@ -118,7 +112,7 @@ class VisualizerView : View { * @param data data */ private fun genSamplingPoint(data: ByteArray) { - if (upShowStyle != ShowStyle.STYLE_WAVE) { + if (mShowStyle != ShowStyle.STYLE_WAVE) { return } if (pointList == null) { @@ -129,7 +123,7 @@ class VisualizerView : View { pointList!!.add(Point(0, 0)) var i = WAVE_SAMPLING_INTERVAL while (i < LUMP_COUNT) { - pointList!!.add(Point(LUMP_SIZE * i, waveData!![i].toInt())) + pointList!!.add(Point(LUMP_SIZE * i, data[i].toInt())) i += WAVE_SAMPLING_INTERVAL } pointList!!.add(Point(LUMP_SIZE * LUMP_COUNT, 0))