0

我正在尝试为我的音乐播放器应用程序创建一个简单的均衡器,在AudioFxDemo之后,我在女巫中创建了一个简单的活动,我有 4 个不同的搜索栏,应该会改变某些乐队的音量。现在我在调用我的 EqualizerActivity 的 MainActivity 中实例化了我的 MediaPlayer 和均衡器,我将均衡器变量设置为公共静态,以便我可以将它放入我的 EqualizerActivity。

//MediaPlayer
        mp = new MediaPlayer();
        equalizer = new Equalizer(0, mp.getAudioSessionId());
        int val = equalizer.setEnabled(true);
        if(val != Equalizer.SUCCESS)
            Log.v("A", "EQUALIZER NON ATTIVO");
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

我的问题是 equalizer.setEnabled(true) 返回 ERROE_INVALID_OPERATION (-5) 而不是 SUCCESS,此时我的代码可以正常工作,但我不能使用均衡器。我真的不知道问题出在哪里,但我认为可能是我将均衡器设置为静态,是另一种将均衡器传递给 EqualizerActivity 的方法吗?

public class EqualizerActivity extends Activity {

private Equalizer equalizer;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    equalizer = MainActivity.equalizer; //preso dall'activity -> sempre attivo
    int val = equalizer.setEnabled(true);
    if(val != Equalizer.SUCCESS)
        Log.v("A", "EQUALIZER NON ATTIVO " + val);
    setupEqualizerFXandUI();

}


//generato dinamicamente a seconda delle bande percepite
private void setupEqualizerFXandUI()
{
    TextView eqTextView = new TextView(this);
    eqTextView.setText("Equalizer:");
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.addView(eqTextView);

    setContentView(ll);

     short bands = equalizer.getNumberOfBands();
     final short minEQLevel = equalizer.getBandLevelRange()[0];
     final short maxEQLevel = equalizer.getBandLevelRange()[1];

     for(short i = 0; i< bands; i++)
     {
         final short band = i;
         //Log.v("A", "B "+ band);
         TextView freqTv = new TextView(this);
         freqTv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
         freqTv.setGravity(Gravity.CENTER_HORIZONTAL);
         freqTv.setText((equalizer.getCenterFreq(band) /1000) + " Hz");
         ll.addView(freqTv);

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

         TextView minDbTv = new TextView(this);
         minDbTv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
         minDbTv.setText((minEQLevel / 100) + " dB");

         TextView maxDbTv = new TextView(this);
         maxDbTv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
         maxDbTv.setText((maxEQLevel / 100) + " dB");

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

         SeekBar bar = new SeekBar(this);
         bar.setLayoutParams(layoutParams);
         bar.setMax(maxEQLevel - minEQLevel);
         bar.setProgress(equalizer.getBandLevel(band)); // Volume della banda

         bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                equalizer.setBandLevel(band, (short)(progress + minEQLevel));
                Log.v("A", "LEVEL: " + (progress + minEQLevel));
            }
        });

         row.addView(minDbTv);
         row.addView(bar);
         row.addView(maxDbTv);

         ll.addView(row);

     }

}

}

在 MainActivity 我释放均衡器:

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

        if(mNotify != null)
            mNotify.cancel(001);
        if(mHandler != null)
            mHandler.removeCallbacks(mUpdateTimeTask); //rimuovo il thread che aggiorna la seekbar
        if(mp != null){
            mp.release(); //rilascio il media player
        }
        if(equalizer != null)
            equalizer.release();
        if(bassboost != null)
            bassboost.release();
}
4

2 回答 2

0

好的,似乎 n7player(我正在使用的一个音乐应用程序)有一个进程,它采用最高优先级的均衡器(我认为)。关闭 n7player 进程我可以使用均衡器和低音增强器。

于 2013-05-08T09:12:07.110 回答
0

优先级——均衡器引擎可以被多个应用程序共享。每个应用程序在创建均衡器对象时都会提供优先级。具有最高优先级的应用程序控制引擎。

但我无法找到将均衡器设置为最高优先级的方法。Let me know if someone find a solution.

编辑

我想我找到了解决方案。但我还没有测试过。

@Override
protected void onResume() {
    super.onResume();
    if (mEqualizer.hasControl() == false) {
        prepareEqualizer();//force re-gaining control
    }
}
于 2015-05-08T11:21:35.527 回答