问题
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.
回答1:
In AudioEffect, there are 3 types of error occurs.
- AudioEffect.ERROR_BAD_VALUE
- AudioEffect.ERROR_INVALID_OPERATION -->
this occurs for your case
. - 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:
- https://stackoverflow.com/a/10885407/2293534
- https://stackoverflow.com/a/40968149/2293534
- Analysis of BassBoost.java class
回答2:
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)); }
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.
来源:https://stackoverflow.com/questions/42323140/unsupportedoperationexception-audioeffect-invalid-parameter-operation