Equalizer setEnabled devuelve un error de operación no válida

I'm trying to create a simple equalizer for my music player application, following the AudioFxDemostración I've created a simple activity in witch I've 4 different seekbars that should change the volumes of some bands. For now I have my MediaPlayer and Equalizer instantiated in the MainActivity that calls my EqualizerActivity, I've set the equalizer variable as a public static so that I can get it into my 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);

My problem is that the equalizer.setEnabled(true) returns ERROE_INVALID_OPERATION (-5) instead of SUCCESS, at this point I've got the code working but I can't use the equalizer. I really don't know what's the problem, but I think it could be that I'm setting the equalizer as static, is the another way to pass the equalizer to the 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);

     }

}

}

In the MainActivity I release the equalizer:

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();
}

preguntado el 05 de mayo de 13 a las 11:05

Are you doing any other equalizer operations in MainActivity? -

Also, confirm if this behaviour is on the device or emulator ? If on device, what is the behaviour on emulator? -

Testing on my device (galaxy S2), just tried on the emulator and I still got the error. The only other thing I'm doing with equalizer is declaring it as public static equalizer and releasing it when I close the application. I really can't figure it out -

What exactly you are doing the release of equalizer ? onDestroy? -

sorry, meant Where exactly are you doing the release of equalizer ? onDestroy ? -

2 Respuestas

Ok, seems like that n7player (a music application I'm using) had a process that took the equalizer with the highest priority (I think). Closing the n7player process I'm able to use the equalizer and the bass booster.

contestado el 08 de mayo de 13 a las 10:05

the priority – an equalizer engine can be shared by a number of apps. Each app provides a priority when they create their equalizer object. The app with the highest priority controls the engine.

But i am unable to find a way to set my Equalizer with highest priority.Let me know if someone find a solution.

Editar

I think i find a solution.but i haven't tested it yet.

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

contestado el 08 de mayo de 15 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.