UnsupportedOperationException AudioEffect: invalid parameter operation

吃可爱长大的小学妹 提交于 2019-12-08 18:47:56

问题


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.

  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



回答2:


  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.



来源:https://stackoverflow.com/questions/42323140/unsupportedoperationexception-audioeffect-invalid-parameter-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!