Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnsupportedOperationException AudioEffect: invalid parameter operation

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.

like image 427
Julia Avatar asked Feb 19 '17 02:02

Julia


1 Answers

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
like image 52
SkyWalker Avatar answered Oct 14 '22 11:10

SkyWalker