设置均衡器中的波段级别不起作用

时间:2016-07-01 07:23:08

标签: java android audio

我正在编写一个应用程序,使用4个MediaPlayer对象同时播放4个mp3文件。我添加了3个自定义旋钮,使用4个均衡器控制低音,中音和高音,每个均衡器连接到其特定的MediaPlayer。我已经验证我正在按照指定将正确的值(以毫巴为单位)传递给setBandLevel,但是在旋转控件时我听不到声音的任何差异。我在下面添加了相关代码。

我注意到的一件事似乎很奇怪,我调用的均衡器方法预计短路,但频率范围上限可以高达6,000,000毫贝。我可能在这里遗漏了一些东西,但是android中的短片是2 ^ 16,这应该意味着64k的上限。

  

public void setBandLevel(短波段,短波段)

代码运行时没有遇到异常,我跟踪所有值以验证每个频率是否在其波段范围内,但是当我旋转控制旋钮时,声音没有变化。我已经在K,L和M上测试过这个结果。

我很欣赏对此的任何见解。

    public void onResume() {
        super.onResume();

        // Load an ad into the AdMob banner view.
        AdView adView = (AdView) getView().findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        adView.loadAd(adRequest);
        //initialize views
        initializeViews();
        setInstrumentNames();

        mediaPlayer1.start();
        mediaPlayer2.start();
        mediaPlayer3.start();
        mediaPlayer4.start();
        // Start the songtime thread and remember its object so it can be stopped legally
        play();
        setEqualizer();
        setupKnobButtons();
    }

    public void setEqualizer() {
        eq1 = new Equalizer(0, mediaPlayer1.getAudioSessionId());
        eq2 = new Equalizer(0, mediaPlayer2.getAudioSessionId());
        eq3 = new Equalizer(0, mediaPlayer3.getAudioSessionId());
        eq4 = new Equalizer(0, mediaPlayer4.getAudioSessionId());
        if (eq1 != null && eq2 != null && eq3 != null && eq4 != null) {
            int z = eq1.setEnabled(true);
            int c = eq2.setEnabled(true);
            int d = eq3.setEnabled(true);
            int e = eq4.setEnabled(true);
            // The number of bands is usually 5 but could be up to 13
            num_bands = eq1.getNumberOfBands();
            // The band range will always return the min and max
            // Gets the level range for use by setBandLevel(short,short)}. The level is expressed in
            // milliBel. Return the band level range in an array of short integers. The first element is the lower
            // limit of the range, the second element the upper limit.
            // --- THIS IS FOR THE FULL RANGE OF ALL BANDS ---///
            range = eq1.getBandLevelRange();
            min_level = range[0];
            max_level = range[1];
            Log.d(TAG_BANDLEVELS, "min level = " + min_level + " max level = " + max_level);

            // Loop through all the bands ( usually 5 ) and create 2 arrays
            // ( one for lower limit of each band and one for the upper )
            for (int i = 0; i < num_bands; i++) {
                freq_range = eq1.getBandFreqRange((short) i);
                freqRangeLowerLimit.add(freq_range[0]);
                freqRangeUpperLimit.add(freq_range[1]);
                    Log.d(TAG_BANDLEVELS, "freqRangeLowerLimit = " + freq_range[0] +
                        " freqRangeUpperLimit = " + freq_range[1] + " band  = " + i);
            }
        }
    }
    private void setBandLevels(int percentage, int new_level1, int new_level2, int nBand) {
        if (eq1.hasControl()) {
            eq1.setBandLevel((short) nBand, (short) new_level1);
        }
        else
            Log.d(TAG_BANDLEVELS, "EQ 1 DOES NOT HAVE CONTROL");
        if (eq2.hasControl()) {
            eq2.setBandLevel((short) nBand, (short) new_level1);
        }
        else
            Log.d(TAG_BANDLEVELS, "EQ 2 DOES NOT HAVE CONTROL");
        if (eq3.hasControl()) {
            eq3.setBandLevel((short) nBand, (short) new_level1);
        }
        else
            Log.d(TAG_BANDLEVELS, "EQ 3 DOES NOT HAVE CONTROL");
        if (eq4.hasControl()) {
            eq4.setBandLevel((short) nBand, (short) new_level1);
        }
        else
            Log.d(TAG_BANDLEVELS, "EQ 4 DOES NOT HAVE CONTROL");
        // Mid and Treble use 2 bands (Mid = 2nd and 3rd Treble = 4th and 5th
        if (nBand == PlayFragment.BAND_BASS || nBand == PlayFragment.BAND_TREBLE) {
            try {
                eq1.setBandLevel((short) (nBand+1),  (short) new_level2);
                eq2.setBandLevel((short) (nBand+1),  (short) new_level2);
                eq3.setBandLevel((short) (nBand+1),  (short) new_level2);
                eq4.setBandLevel((short) (nBand+1),  (short) new_level2);
            } catch (Throwable ex2) {
                ex2.printStackTrace();
            }
        }
    }

    public void setupKnobButtons() {
        RoundKnobButton rnbBass = (RoundKnobButton) getView().findViewById(R.id.btRoundBass);
        rnbBass.SetListener(new RoundKnobButtonListener() {
            TextView tvEQBass = (TextView) getView().findViewById(R.id.tvEQBass);

            public void onStateChange(boolean newstate) {
                Toast.makeText(getActivity(), "New state:" + newstate, Toast.LENGTH_SHORT).show();
            }

            public void onRotate(final int percentage) {
                tvEQBass.post(new Runnable() {
                    public void run() {
                        tvEQBass.setText("\n" + percentage + "%\n");
                        // Bass only uses the first band
                        int lower1 =  freqRangeLowerLimit.get(0);
                        int upper1 =  freqRangeUpperLimit.get(0);
                        int new_level1  = lower1 + ((upper1 - lower1) * percentage/100);
    //                    new_level1 /= 1000;
    //                    int new_level1 = calc1/100;
                        int lower2 =  freqRangeLowerLimit.get(1);
                        int upper2 =  freqRangeUpperLimit.get(1);
                        int new_level2  = lower1 + ((upper2 - lower2) * percentage/100);
   //                     new_level2 /= 1000;
  //                      int new_level2 = calc2/100;
                        setBandLevels(percentage, new_level1, new_level2, BAND_BASS);
                        Log.d(TAG_BANDLEVELS, "Band = Bass " +  "New Level1 = " + new_level1 + "New Level2 = " + new_level2 + " Percentage = " + percentage);
                    }
                });
            }
        });

0 个答案:

没有答案