均衡器setEnabled返回无效的操作错误

时间:2013-05-05 10:13:40

标签: android equalizer

我正在尝试为我的音乐播放器应用程序创建一个简单的均衡器,跟随AudioFxDemo我创建了一个简单的活动,我有4个不同的搜索栏,应该改变一些乐队的音量。 现在我将我的MediaPlayer和Equalizer在MainActivity中实例化,调用我的EqualizerActivity,我将均衡器变量设置为公共静态,以便我可以将它变为我的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);

我的问题是,均衡器.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();
}

2 个答案:

答案 0 :(得分:0)

好吧,好像,n7player(我正在使用的音乐应用程序)有一个过程,它使均衡器具有最高优先级(我认为)。关闭n7player过程我可以使用均衡器和低音助推器。

答案 1 :(得分:0)

  

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

但我无法找到一种方法来设置我的均衡器具有最高优先级Let me know if someone find a solution.

修改

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

@Override
protected void onResume() {
    super.onResume();
    if (mEqualizer.hasControl() == false) {
        prepareEqualizer();//force re-gaining control
    }
}