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)

anon
  • 855
  • 12
  • 22
AkhilGite
  • 400
  • 4
  • 17
  • 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? – Vince VD Aug 26 '19 at 15:49

2 Answers2

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().

Shubham
  • 165
  • 2
  • 11
  • how is that possible? same code gives preset for android version below nougat and not giving preset on nougat – AkhilGite Dec 05 '16 at 08:02
  • i have already added getNumberOfPresets() before that to initialize spinner adapter – AkhilGite Dec 05 '16 at 08:25
  • Try this `MusicPlayer.getEqualizerHelper().getCurrentEqualizer().usePreset(getCurrentPreset());` – Shubham Dec 05 '16 at 08:45
  • 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 error – AkhilGite Dec 05 '16 at 09:16
  • I used getCurrentPrese‌​t() still I am getting the same error. – AkhilGite Dec 05 '16 at 09:29
  • No, I am running it on real device. – AkhilGite Dec 05 '16 at 09:55
  • Check this [link](http://stackoverflow.com/questions/10536170/equalizer-not-always-supported-even-when-api-9) – Shubham Dec 05 '16 at 10:00
  • 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 25 – AkhilGite Dec 05 '16 at 10:22
  • Read again. He is not saying **all devices**. Try making the equalizer object equal to null after releasing it. – Shubham Dec 05 '16 at 10:34
  • Equalizer throwing exception before it reaches release, this is occurring on android api level 25 only. – AkhilGite Dec 05 '16 at 12:01
  • 3
    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 @Shubham for your response. You can check my working code [here](https://play.google.com/store/apps/details?id=com.ag.musicplayer) – AkhilGite Jan 12 '17 at 05:58
  • @AkhilGite I will definitely have a look and give u the feedback. Nice work btw :) – Shubham Jan 12 '17 at 06:59
  • @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? – Stanislav Nov 27 '17 at 15:08
  • 1
    @Stanislav 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. – AkhilGite Nov 28 '17 at 06:38
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.

  • 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. – Vince VD Aug 26 '19 at 15:38