1

I'm playing with Equalizer and found that whenever I'm starting my application and then using another equalizer app my app crashes on attempt to use anything (e.g. setBandLevel).

java.lang.UnsupportedOperationException: AudioEffect: invalid parameter operation at android.media.audiofx.AudioEffect.checkStatus(AudioEffect.java:1244) at android.media.audiofx.Equalizer.setBandLevel(Equalizer.java:230)

I'm listening to the onControlStatusChange and I have information that my app lost control over the effect. How can I reclaim it back in onResume?

I assume I could clear effect instance (release then nullify) and instantiate the effect class again v(like in code example below), but was wondering if there is something like setControlState(true) built in functionality.

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

private void prepareEqualizer() {
    if (mEqualizer != null) {
        releaseEqualizer();
    }
    mEqualizer = new Equalizer(0, 0);
    mEqualizer.setEnabled(true);
    mEqualizer.setControlStatusListener(this);
    mEqualizer.setEnableStatusListener(this);
    mEqualizer.setParameterListener(this);
    mMaxBands = mEqualizer.getNumberOfBands();
    mGainLevels = mEqualizer.getBandLevelRange();

}

private void releaseEqualizer() {
    if (mEqualizer == null)
        return;
    mEqualizer.setControlStatusListener(null);
    mEqualizer.setEnableStatusListener(null);
    mEqualizer.setParameterListener(null);
    mEqualizer.release();
    mEqualizer = null;
}
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79

2 Answers2

0

If your app is in the background when it crashes, then most likely, you should relinquish the equalizer in onPause() which gets called before your app goes in background and another app comes in foreground.

@Override
protected void onPause() {
  super.onPause();
  if (mEqualizer != null) {
    // do what you have to do to free and de-alloc mEqualizer
  }
}

and then in onResume() -

@Override
protected void onResume() {
  super.onResume();
  if (mEqualizer == null) {
    // create new instance
    // initialize
    // use it
  }
}
VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
  • thanks for your support, but it crashes not because of 'null' reference. I'll update my question. – Lukasz 'Severiaan' Grela Nov 02 '14 at 01:08
  • correct me if i am wrong - only one app can have control of the hardware like Equalizer at any given time. So, if the other app got control of it, then when your app becomes "active", you will have to re-create the new instance, right? I don't think saving the state in onSaveInstanceState and restoring it will help either. – VJ Vélan Solutions Nov 02 '14 at 01:24
  • I think the same, so the question is "if there is something like `setControlState(true)` built in functionality". Probably there isn't and I can only check if existing instance `hasControl` and if not recreate it with new instance. – Lukasz 'Severiaan' Grela Nov 02 '14 at 01:39
  • i don't think there is `setControlState()` or it's equivalent as per docu. The closest i came is setting the `priority` value really high so that the other app does not get control :) `mEqualizer = new Equalizer(0, 0);` – VJ Vélan Solutions Nov 02 '14 at 01:55
0

The short answer is: you can't, but it works with code I've already posted in teh question:

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

private void prepareEqualizer() {
    if (mEqualizer != null) {
        releaseEqualizer();
    }
    mEqualizer = new Equalizer(0, 0);
    mEqualizer.setEnabled(true);
    mEqualizer.setControlStatusListener(this);
    mEqualizer.setEnableStatusListener(this);
    mEqualizer.setParameterListener(this);
    mMaxBands = mEqualizer.getNumberOfBands();
    mGainLevels = mEqualizer.getBandLevelRange();

}

private void releaseEqualizer() {
    if (mEqualizer == null)
        return;
    mEqualizer.setControlStatusListener(null);
    mEqualizer.setEnableStatusListener(null);
    mEqualizer.setParameterListener(null);
    mEqualizer.release();
    mEqualizer = null;
}
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79