UnsupportedOperationException AudioEffect: invalid parameter operation
Asked Answered
R

2

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.

Resinate answered 19/2, 2017 at 2:56 Comment(6)
@Henry catching an exception is always the last thing to consider if you could fix the issue using control conditionsPave
@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.Cohla
@Cohla 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.Pave
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.Electrodeposit
@MeenaPintu how did you disable the default equalizer?Elledge
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.@VinceElectrodeposit
L
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://mcmap.net/q/881284/-equalizer-not-always-supported-even-when-api-gt-9
  2. https://mcmap.net/q/908012/-java-lang-unsupportedoperationexception-audioeffect-invalid-parameter-operation-exception-on-android-nougat-7-0
  3. Analysis of BassBoost.java class
Loxodromics answered 29/4, 2017 at 17:45 Comment(0)
S
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.

Surgical answered 6/5, 2017 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.