17

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?

Jona
  • 13,325
  • 15
  • 86
  • 129
  • 1
    If you down vote at least have the time to comment why you down voted! – Jona Aug 14 '12 at 18:45
  • Someone just went around downvoting every bounty'd question and its answers... I really hope they get banned. – Cat Aug 15 '12 at 17:48
  • Hello Jona, Have you got any solution of this. I also want to implement audio effect in to the app but got the error as above. – Shreyash Mahajan Apr 12 '13 at 03:57

4 Answers4

5

You should look at: this

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

RE6
  • 2,684
  • 4
  • 31
  • 57
  • Thanks for the link but I actually posted that bug :P – Jona Aug 18 '12 at 13:29
  • @Android Joker: I have seen the link. I know that the issue is still not resolve but what if i would like to implement the AudioEffect like Bass, trouble and Equalizer? Please provide any demo which works all over. – Shreyash Mahajan Apr 12 '13 at 03:55
3

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);
}
Michael
  • 57,169
  • 9
  • 80
  • 125
  • i have seen your answer second time on SO. Can you please help with some of more description and java code to solve this matter? – Shreyash Mahajan Apr 12 '13 at 04:12
  • I haven't worked with any of the devices that the OP was having issues with, so I can't really help with any solution. I added my answer only to address the risk of using audio session 0, since that was a part of the question. – Michael Apr 12 '13 at 08:49
1

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

Diljeet
  • 1,896
  • 20
  • 24
0

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.

yano
  • 4,095
  • 3
  • 35
  • 68