java.lang.UnsupportedOperationException: AudioEffect: invalid parameter operation exception on Android Nougat (7.0)
Asked Answered
O

2

1

I have written below code in onCreate method of activity.

MusicPlayer.getEqualizerHelper().getCurrentEqualizer().usePreset((short) 0); --- line no 1
short numberFrequencyBands = MusicPlayer.getEqualizerHelper().getCurrentEqualizer().getNumberOfBands();--- line no 2
final short lowerEqualizerBandLevel = MusicPlayer.getEqualizerHelper().getCurrentEqualizer().getBandLevelRange()[0];--- line no 3

and it works fine in all android o.s below nougat. When I install my app on nougat device it throws exception on line no 1. Please suggest me where is the problem and solution for it. Thanks in advance

FATAL EXCEPTION: main Process: com.ag.musicplayer, PID: 15039 java.lang.RuntimeException: Unable to start activity

ComponentInfo{com.ag.musicplayer/com.ag.musicplayer.activity.EqualizerActivity}: java.lang.UnsupportedOperationException: AudioEffect: invalid parameter operation at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.UnsupportedOperationException: AudioEffect: invalid parameter operation at android.media.audiofx.AudioEffect.checkStatus(AudioEffect.java:1273) at android.media.audiofx.Equalizer.usePreset(Equalizer.java:335) at com.ag.musicplayer.activity.EqualizerActivity.onCreate(EqualizerActivity.java:287) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Outflow answered 5/12, 2016 at 6:1 Comment(1)
How did you resolve this problem? I'm also running my code on LineageOS which is a custom rom, so did you just disable the equalizer for custom roms or is there a workaround for this problem?Foamflower
M
0

I don't exactly know why it is throwing error on Nougat but one possible reason is that there might be no preset available. So to be sure you may check first whether any preset is available or not using getNumberOfPresets().

Mum answered 5/12, 2016 at 6:14 Comment(14)
how is that possible? same code gives preset for android version below nougat and not giving preset on nougatOutflow
i have already added getNumberOfPresets() before that to initialize spinner adapterOutflow
Try this MusicPlayer.getEqualizerHelper().getCurrentEqualizer().usePreset(getCurrentPreset());Mum
I want preset which user selects from spinner. So I am setting spinner adapter by getNumberOfPresets() and onItemClick I am calling MusicPlayer.getEqualizerHelper().getCurrentEqualizer().usePreset((short) 0); but it's giving errorOutflow
I used getCurrentPrese‌​t() still I am getting the same error.Outflow
No, I am running it on real device.Outflow
Check this linkMum
I already check this answer and I used release(). And in that que he is saying all the devices above api>=9 but I have problem with api level 25. My app is working fine on all the devices except api level 25Outflow
Read again. He is not saying all devices. Try making the equalizer object equal to null after releasing it.Mum
Equalizer throwing exception before it reaches release, this is occurring on android api level 25 only.Outflow
Finally, I got the solution to problem. This error occurs if you are running your code on custom rom. I always tried on custom rom. When I check it on android N it didn't crashed. Thank you @Mum for your response. You can check my working code hereOutflow
@Outflow I will definitely have a look and give u the feedback. Nice work btw :)Mum
@AkhilGite, what is your solution? The link you posted directs to Play Market, not to the code. Are you just disable equalizer on the custom roms?Cuprous
@Cuprous Hey, Actually the code doesn't work for Nougat onwards so I handled the exception on Nougat onwards and that's what rest of the players are doing.Outflow
V
0

If you run on custom ROM (but also other legacy rom that use a system Equalizer), you must disable system equalizer for your audio session id:

    private  void unbindSystemEqualizer(int audioSessionId) {
    Intent intent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSessionId);
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, mContext.getPackageName());
    mContext.sendBroadcast(intent);
}

Have sure that your custom Equalizer is enabled and a settings was available before launching broadcast intent This work for me.

Vaulting answered 19/8, 2018 at 13:21 Comment(1)
And what if i'm not running a custom rom? Is there some way to know if someone is running one? After i flashed lineageOS i had these error.Foamflower

© 2022 - 2024 — McMap. All rights reserved.