7

I'm getting an UnsupportedOperationException error on an equalizer on this line of code. bassBoost.setStrength((short) bassBoostPos);

Here's the code

equalizer = new Equalizer(0, 0);
if (equalizer != null) {
equalizer.setEnabled (isEqualizer);
numBands = equalizer.getNumberOfBands();
short r[] = equalizer.getBandLevelRange();
minLevel = r[0];
maxLevel = r[1];
bassBoost = new BassBoost (0, 0);

if(bassBoost != null) {
    bassBoost.setEnabled(bassBoostPos > 0 ? true : false);
    bassBoost.setStrength((short) bassBoostPos); 
}

Here's the exception

java.lang.UnsupportedOperationException: AudioEffect: invalid parameter    
operation
at android.media.audiofx.AudioEffect.checkStatus(AudioEffect.java:1271)
at android.media.audiofx.BassBoost.setStrength(BassBoost.java:127)

How do I fix this so that the application doesn't crash. I mean how can I check to see if the device support this operation, if it doesn't support, I would just skip this line. Thanks.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Julia
  • 1,207
  • 4
  • 29
  • 47
  • @Henry catching an exception is always the last thing to consider if you could fix the issue using control conditions – Jan Heinrich Reimer Apr 29 '17 at 13:57
  • @Heinrich an `UnsupportedOperationException` is typically thrown by a concrete implementation of an interface or abstract class if it decides not to implement a certain operation. A client doesn't know if the concrete implementation supports the operation until it calls it. Otherwise it would have to have knowledge about all existing (and future) concrete implementations. – Henry Apr 29 '17 at 14:22
  • @Henry The thing with Android's `AudioEffect` classes is that sometimes they work perfectly well and sometimes they don't and instead throw the exception even on the same device. – Jan Heinrich Reimer Apr 29 '17 at 14:48
  • I also got same exception but after disabling default equalizer (in my case audiofx on lineage os ) it worked perfectly . so I think the error is due to already installed equalizer . and Yes in my Case `bassBoost.setEnabled(bassBoostPos > 0 ? true : false);` was returning -5 when another system equalizer was enabled else 0 . same for Equalizer set enabled() . a temporary solution if system has equalizer then call it else use yours. – Meena Pintu Jul 10 '17 at 08:44
  • @MeenaPintu how did you disable the default equalizer? – Vince VD Aug 26 '19 at 17:03
  • I had a pre installed equalizer so couldn't uninstall it but can be disabled manually by android settings. I didn't try to find any programmatic solution for it , I used the solution that if there is an equalizer installed, open it up else open yours.@Vince – Meena Pintu Aug 27 '19 at 05:47

2 Answers2

4

In AudioEffect, there are 3 types of error occurs.

  1. AudioEffect.ERROR_BAD_VALUE
  2. AudioEffect.ERROR_INVALID_OPERATION --> this occurs for your case.
  3. RuntimeException

Why AudioEffect.ERROR_BAD_VALUE occurs?

Operation failed due to bad parameter value. It causes IllegalArgumentException and gives the error "AudioEffect: bad parameter value"

Why AudioEffect.ERROR_INVALID_OPERATION occurs?

Operation failed because it was requested in wrong state. It causes UnsupportedOperationException and gives the error "AudioEffect: invalid parameter operation"

RuntimeException

It occurs in runtime. It gives the error "AudioEffect: set/get parameter error"

When wrong state happens mainly? How to make solution?

Ans: After finishing process of an equalizer, if it doesn't called the release() method, the wrong state happens. So make the equalizer object equal to null after releasing it.

If you use api level 25, then change it. This error occurs in this level mostly. So, if possible, change it.

Sometimes instantiation of a new AudioEffect is not allowed by native libraries. because too many objects are already exists there. It also causes wrong state.

Resource Link:

  1. https://stackoverflow.com/a/10885407/2293534
  2. https://stackoverflow.com/a/40968149/2293534
  3. Analysis of BassBoost.java class
Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
0
  1. Before setting the strength it needs to be checked if its supported or not. To do that the below condition needs to be added.

    if(bassBoost.getStrengthSupported()) { bassBoost.setStrength((short) bassBoostPos)); }

  2. Additional note is that the value of BassBoost strength should be in the range of 0 to 1000 indicating the mildest effect to strongest effect.

OTM
  • 656
  • 5
  • 8