This article describes the Android player MediaPlayer's equalizer effect. Share it for your reference, as follows:
These days, I have been learning Android's official API Demos in the system. When I saw the effect of implementing the equalizer, I copied the code in the official API, and slightly modified it according to the guidance of my seniors on the Internet, and added comments.
public class AudioFxDemo extends Activity { private static final String TAG = "AudioFxDemo"; private static final float VISUALIZER_HEIGHT_DIP = 50f; // Define the player private MediaPlayer mMediaPlayer; // Define the frequency spectrum of the system private Visualizer mVisualizer; // Define the equalizer for the system private Equalizer mEqualizer; private LinearLayout mLinearLayout; private VisualizerView mVisualizerView; private TextView mStatusTextView; @Override public void onCreate(Bundle bundle) { (bundle); // Volume control setVolumeControlStream(AudioManager.STREAM_MUSIC); mStatusTextView = new TextView(this); mLinearLayout = new LinearLayout(this); (); (mStatusTextView); setContentView(mLinearLayout); // Create MediaPlayer object mMediaPlayer = (this, .test_cbr); (TAG, "MediaPlayer audio session ID: " + ()); // Set the spectrum display setupVisualizerFxAndUI(); // Set the oscilloscope display setupEqualizerFxAndUI(); // Make sure the visualizer is enabled only when you actually want to // receive data, and // when it makes sense to receive data. (true); // When the stream ends, we don't need to collect any more data. We // don't do this in // setupVisualizerFxAndUI because we likely want to have more, // non-Visualizer related code // in this callback. mMediaPlayer .setOnCompletionListener(new () { public void onCompletion(MediaPlayer mediaPlayer) { (false); ("Play ends"); } }); (); ("Playing"); } private void setupEqualizerFxAndUI() { // Create the Equalizer object (an AudioEffect subclass) and attach it // to our media player, // with a default priority (0). mEqualizer = new Equalizer(0, ()); (true); TextView eqTextView = new TextView(this); ("Equalizer:"); (eqTextView); short bands = (); final short minEQLevel = ()[0]; final short maxEQLevel = ()[1]; for (short i = 0; i < bands; i++) { final short band = i; TextView freqTextView = new TextView(this); (new ( .MATCH_PARENT, .WRAP_CONTENT)); (Gravity.CENTER_HORIZONTAL); (((band) / 1000) + " Hz"); (freqTextView); LinearLayout row = new LinearLayout(this); (); TextView minDbTextView = new TextView(this); (new ( .WRAP_CONTENT, .WRAP_CONTENT)); ((minEQLevel / 100) + " dB"); TextView maxDbTextView = new TextView(this); (new ( .WRAP_CONTENT, .WRAP_CONTENT)); ((maxEQLevel / 100) + " dB"); layoutParams = new ( .MATCH_PARENT, .WRAP_CONTENT); = 1; SeekBar bar = new SeekBar(this); (layoutParams); (maxEQLevel - minEQLevel); ((band)); (new () { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { (band, (short) (progress + minEQLevel)); } public void onStartTrackingTouch(SeekBar seekBar) { } public void onStopTrackingTouch(SeekBar seekBar) { } }); (minDbTextView); (bar); (maxDbTextView); (row); } } private void setupVisualizerFxAndUI() { // Create a VisualizerView (defined below), which will render the // simplified audio // wave form to a Canvas. mVisualizerView = new VisualizerView(this); (new ( .MATCH_PARENT, (int) (VISUALIZER_HEIGHT_DIP * getResources() .getDisplayMetrics().density))); (mVisualizerView); // Create the Visualizer object and attach it to our media player. mVisualizer = new Visualizer(()); (()[1]); ( new () { public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) { (bytes); } public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) { } }, () / 2, true, false); } @Override protected void onPause() { (); if (isFinishing() && mMediaPlayer != null) { (); (); (); mMediaPlayer = null; } } } /** * Drawing a wavy view * * @description: * @author ldm * @date 2016-4-20 9:11:49 AM */ class VisualizerView extends View { // The array saves the value of the waveform sampling point private byte[] bytes; private float[] points; // Define the brush private Paint paint = new Paint(); // Rectangular area private Rect rect = new Rect(); private byte type = 0; public VisualizerView(Context context) { super(context); bytes = null; // Set the properties of the brush (1f);// Set the hollow line width (true);// Anti-aliasing ();// Brush color ();// Non-fill mode } public void updateVisualizer(byte[] ftt) { bytes = ftt; // Notify component to repaint invalidate(); } @Override public boolean onTouchEvent(MotionEvent me) { // When the user touches the component, switch the waveform type if (() != MotionEvent.ACTION_DOWN) { return false; } type++; if (type >= 3) { type = 0; } return true; } @Override protected void onDraw(Canvas canvas) { (canvas); if (bytes == null) { return; } // Draw a black background (); // Use rect object to record the width and height of the component (0, 0, getWidth(), getHeight()); switch (type) { // Draw blocky waveforms case 0: for (int i = 0; i < - 1; i++) { float left = getWidth() * i / ( - 1); // Calculate the height of the rectangle based on the waveform value float top = () - (byte) (bytes[i + 1] + 128) * () / 128; float right = left + 1; float bottom = (); (left, top, right, bottom, paint); } break; // Draw a columnar waveform diagram (draw a rectangle every 18 sampling points) case 1: for (int i = 0; i < - 1; i += 18) { float left = () * i / ( - 1); // Calculate the height of the rectangle based on the waveform value float top = () - (byte) (bytes[i + 1] + 128) * () / 128; float right = left + 6; float bottom = (); (left, top, right, bottom, paint); } break; // -Draw curve waveform diagram case 2: // If the point array has not been initialized if (points == null || < * 4) { points = new float[ * 4]; } for (int i = 0; i < - 1; i++) { // Calculate the x-coordinate of the i-th point points[i * 4] = () * i / ( - 1); // Calculate the y-coordinate of the i-th point based on the value of bytes[i] (the value of the waveform point). points[i * 4 + 1] = (() / 2) + ((byte) (bytes[i] + 128)) * 128 / (() / 2); // Calculate the x coordinate of point i+1 points[i * 4 + 2] = () * (i + 1) / ( - 1); // Calculate the y-coordinate of the i+1th point based on the value of bytes[i+1] (value of the waveform point) points[i * 4 + 3] = (() / 2) + ((byte) (bytes[i + 1] + 128)) * 128 / (() / 2); } // Draw the waveform curve (points, paint); break; } } }
When creating a new project by yourself, remember to add an mp3 format file named test_cbr under res/raw.
For more information about Android related content, please check out the topic of this site:Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.