androidequalizeraudioeffect

Equalizer - how to regain control state


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

Solution

  • 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;
    }