Using audioSessionId value to instantiate AudioFx

2020-07-01 15:58发布

I have read the Android APIs and tried searching over the internet about declaring a custom audioSessionId and then using that audioSessionId to initialize an AudioFx class and assign my MediaPlayer or AudioTrack the hardcoded audioSessionId.

This method would allow me to create an AudioFx first and later attach a new MediaPlayer or AudioTrack to this audioSessionId.

I'm currently able to use this method on Android 2.3.6 but on Android 4.x I'm running into issues with errors that initialization fails or on other ICS/JellyBean devices this error is silent but calling a function leads to exceptions.

Samsung Galaxy S II [Android 4.0.3]: [Issue no longer happens with Android 4.0.4]

 E/AudioEffect(13250): set(): AudioFlinger could not create effect, status: -38
 E/AudioEffects-JNI(13250): AudioEffect initCheck failed -5
 E/AudioEffect-JAVA(13250): Error code -5 when initializing AudioEffect.
 W/WrapEqualizer(13250): createEqualizer() -> Effect library not loaded

Motorola Xoom [Android 4.1.2]
Fails it seems silently after the constructor. Then calling on getProperties() it crashes.

java.lang.RuntimeException: AudioEffect: set/get parameter error
    at android.media.audiofx.AudioEffect.checkStatus(AudioEffect.java:1247)
    at android.media.audiofx.Equalizer.getProperties(Equalizer.java:532)

Nexus 4 [Android 4.2.1]
Using audioSessionId=0 everything works fine but using any other number the device will report the following silent error every time I try to change the preset, band level, bass boost to ON or Virtualizer to ON. The effect ID reported is different depending on the FX I'm trying to modify.

W/AudioPolicyManagerBase(165): unregisterEffect() unknown effect ID 1381

Update 08/11/12:
I'm able to use audioSessionId as 0. I know it's deprecated but it works using the permission. <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> Should I be using the AudioFx with the audio session id 0?

标签: android audio
4条回答
一夜七次
2楼-- · 2020-07-01 16:31

i know this issue if somebody want to try

do this

 Equalizer eq=null;
 .
 .
 .
 .
 .
 //in any function before initialization do this
 if(eq!=null)
      eq.release();
 eq=new Equalizer(0, audiosessionid);

try it once

查看更多
一夜七次
3楼-- · 2020-07-01 16:34

You should look at: this

Apparently it is an unsolved issue came up in ICS, and probably wasn't solved either in JB.

查看更多
闹够了就滚
4楼-- · 2020-07-01 16:35

Should I be using the AudioFx with the audio session id 0?

It will probably work in some cases, but don't count on it to continue to do so on future Android versions. You'll already be compromising interoperability between your app and other apps on Jellybean. Just take a look at what the AudioFlinger does when an effect is enabled:

// suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
// another session. This gives the priority to well behaved effect control panels
// and applications not using global effects.
// Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
// global effects
if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) {
    setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
}
查看更多
唯我独甜
5楼-- · 2020-07-01 16:35

Other than session 0 which is the "deprecated global session", my understanding of the AudioFlinger code shows that sessions are only created for classes which actually do audio IO, that is, AudioRecord, AudioTrack, MediaPlayer etc. You should create these classes, and then get their session ID, and then attach the effect.

Any other value you supply for session ID will correspond to an audio session that does not exist, and so will fail.

查看更多
登录 后发表回答