java.lang.UnsupportedOperationException: AudioEffe

2019-01-27 06:26发布

问题:

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)

回答1:

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



回答2:

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.