如何使用我的mediaplayer活动实现均衡器

时间:2016-12-17 06:38:42

标签: android equalizer

我在媒体播放器中创建了我的在线广播。我必须为可变声音效果设置均衡器,如低音,高音,混响等,而我的媒体播放器主要是Acitvity。如何在我的主要活动中实现包含均衡器的活动?

仅限搜索条的我的均衡器:

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 个答案:

答案 0 :(得分:0)

  

Crate a 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
  

链接

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