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

2 回答 2

0

简短的回答是:你不能,但它适用于我已经在问题中发布的代码:

@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;
}
于 2014-11-06T14:31:53.307 回答
0

如果您的应用程序在崩溃时处于后台,那么您很可能应该放弃onPause()在您的应用程序进入后台并且另一个应用程序进入前台之前调用的均衡器。

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

然后在 onResume() -

@Override
protected void onResume() {
  super.onResume();
  if (mEqualizer == null) {
    // create new instance
    // initialize
    // use it
  }
}
于 2014-11-02T00:37:37.210 回答