I need to play a sound with the phone speaker even if the earphones are plugged in.
Currently I'm using the SoundPool Play function, but sound get played into the earphones, when the connector is plugged in.
I've found this thread
hello, i need to improve my program, a sort of to-do-list since now, my prog does this: plays ringtone to the loudspeaker through media player and the fake green and red buttons are used to redirect the user to other app pages. the user must read. now, i want the user to answer the fake...
www.b4x.com
but it seems to deal with actual phone calls, since it sets the MODE_IN_CALL parameter.
My use case is about playing sounds with no calls ongoing.
Dim rr As Reflector
rr.Target = rr.GetContext
rr.Target = rr.RunMethod2("getSystemService", "audio", "java.lang.String")
rr.RunMethod2("setMode", "3", "java.lang.int")
rr.RunMethod2("setSpeakerphoneOn", value, "java.lang.boolean")
and it works correctly
the original thread code was
B4X:
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
Dim mode As Int
If Value Then mode = 2 Else mode = 0
r.RunMethod2("setMode", mode, "java.lang.int")
End Sub
and this version is not working on a Samsung A20e with Android 11
I can add that I have done the tests on my XIAOMI smartphone with Android 11 and the code works correctly. I did a further test on a REALME device running Android 11