Android Question Playing sounds through speaker even if earphones are plugged

Alessandro71

Well-Known Member
Licensed User
Longtime User
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
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.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
the code in the first thread does not work (anymore?)
this thread has correct code
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
To me they work fine with both JavaObject and Reflector library. I attached an example.

Having a few more details and a piece of code would help us understand the problem.
For example, what version of Android does your device have?
 

Attachments

  • AudioSpeaker.zip
    214.1 KB · Views: 82
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
yes, because your code is
B4X:
    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

different mode and lack of setSpeakerphoneOn
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Maybe a change of Android 11
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
that was my guess also
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

I guess it's not a change from Android 11
 
Last edited:
Upvote 0
Top