how to implement the equalizer with my mediaplayer activity

5.1k views Asked by At

I have created my online radio in media player. I have to set equalizer for the variable sound effects like bass, treble, reverb and so on, and my media player is in main Acitvity. How to implement this activity that contains equalizer with my main activity?

My Equalizer with seek bars only :

public class EqualizerActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
    LinearLayout linear;
    SeekBar seekbar;
    private Equalizer mEqualizer;
    TextView tvequalizer,tvcenfreq,tvmineq,tvmaxeq;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);

        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mediaPlayer = MediaPlayer.create(this,R.raw.mukilmusic);

        setupEqualizerFxAndUI();
        mediaPlayer.setLooping(true);    

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mediaPlayer) {
//                mVisualizer.setEnabled(false);
            }
        });
        mediaPlayer.start();
//        tvaudio.setText("Playing audio...");    
    }

    private void setupEqualizerFxAndUI() {
        mEqualizer = new Equalizer(0, mediaPlayer.getAudioSessionId());
        mEqualizer.setEnabled(true);

        linear = (LinearLayout)findViewById(R.id.linear) ;
         tvequalizer = (TextView)  findViewById(R.id.tvequalizer);
        tvequalizer.setText("Equalizer:");
//        linear.addView(tvequalizer);

        short bands = mEqualizer.getNumberOfBands();
        final short minEQLevel = mEqualizer.getBandLevelRange()[0];
        final short maxEQLevel = mEqualizer.getBandLevelRange()[1];
        for (short i = 0; i < bands; i++) {
            final short band = i;

            tvcenfreq = (TextView) findViewById(R.id.tvcenfreq);
        tvcenfreq.setText((mEqualizer.getCenterFreq(band) / 1000) + " Hz");
//          linear.addView(tvcenfreq);

            LinearLayout row = new LinearLayout(this);
            row.setOrientation(LinearLayout.HORIZONTAL);

            tvmineq =  new TextView(this);
            tvmineq.setText((minEQLevel/ 100) + " db");
           // linear.addView(tvmineq);

            tvmaxeq =  new TextView(this);
            tvmaxeq.setText((maxEQLevel/ 100) + " db");
        //   linear.addView(tvmaxeq);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            layoutParams.weight = 1;
            seekbar = new SeekBar(this);
            seekbar.setLayoutParams(layoutParams);
            seekbar.setMax(maxEQLevel - minEQLevel);
            seekbar.setProgress(mEqualizer.getBandLevel(band));
            seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                public void onProgressChanged(SeekBar seekBar, int progress,
                                              boolean fromUser) {
                    mEqualizer.setBandLevel(band, (short) (progress + minEQLevel));
                }
                public void onStartTrackingTouch(SeekBar seekBar) {}
                public void onStopTrackingTouch(SeekBar seekBar) {}
            });
            row.addView(tvmineq);
            row.addView(seekbar);
            row.addView(tvmaxeq);
            linear.addView(row);
        }    
    }
    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing() && mediaPlayer != null) {
//          mVisualizer.release();
            mEqualizer.release();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
1

There are 1 answers

0
Rajesh On BEST ANSWER

Crate an Equalizer

Equalizer equalizer = new Equalizer(0,mediaplayer.getAudioSessionId());
equalizer.setEnabled(true);
equalizer.getNumberOfBands(); //it tells you the number of equalizer in device.
equalizer.getNumberOfPresets();//like Normal Classic,Dance Flat,Folk Heavy Metal,Hip Hop,Jazz, Pop, Rock

Link

Android Visualizer implementation crash https://developer.android.com/reference/android/media/audiofx/Equalizer.html http://www.101apps.co.za/articles/perfect-sound-using-the-equalizer-effect-a-tutorial.html