how to implement the equalizer with my mediaplayer activity

时光毁灭记忆、已成空白 提交于 2019-12-13 22:02:57

问题


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:


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



来源:https://stackoverflow.com/questions/41196054/how-to-implement-the-equalizer-with-my-mediaplayer-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!