media player (and text to speech) output to phone ear speaker instead of loudspeaker

pierpa

Member
Licensed User
Longtime User
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 call, listening at the internal ear speaker from the tts and then he can read/tap the infos

please, help me to instruct the media player to play to the ear speaker.

regards

ppp
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Process_Globals
   Dim mp As MediaPlayer
End Sub

Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
   mp.Initialize
   mp.Load(File.DirAssets, "break.mp3")
End Sub

Sub SetEarPhone(Value As Boolean)
   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

Sub Activity_Click
   mp.Play
End Sub

Sub Activity_Resume
   SetEarPhone(True)
End Sub

Sub Activity_Pause(UserClosed As Boolean)
   SetEarPhone(False)
   mp.Pause
End Sub

You should add this line to the manifest editor:
B4X:
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)

It is important to return to the normal state when your app quits (as done in the example code).
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Cool. I'm assuming there are other modes too? I had some code I found in my little repository here from another thread, but I believe someone had commented it didn't work. If it doesn't work, are their similar modes for this method?

B4X:
Dim rf As Reflector
rf.Target = mp
rf.Target = rf.GetField("mp")
rf.RunMethod2("setAudioStreamType","4","java.lang.int")

STREAM_VOICE_CALL = 0
STREAM_SYSTEM = 1
STREAM_RING = 2
STREAM_MUSIC = 3
STREAM_ALARM = 4
STREAM_NOTIFICATION = 5
STREAM_DTMF = 8
 
Upvote 0

pierpa

Member
Licensed User
Longtime User
problem with samsung s3

hello

the code works perfectly with sony xperia

with samsung s3, a subsequent phone call has messed audio.

i add a test project.

the test app, on xperia works as intended, while on samsung s3 the phone called audio is covered with terrible noise.

i want that the second button feature would work

warning: on samsung s3 the second button test can damage your ear!

please, help

regards

ppp

p.s. the attached project hasn't apk inside of it for uploading problems.
the apk is attached apart

p.s. the "187" number used for testing is the free number in italy for operator problems
 

Attachments

  • problema-samsung-telefonata-dopo-tts.zip
    270.6 KB · Views: 685
  • test.apk
    129.4 KB · Views: 505
Last edited:
Upvote 0

pierpa

Member
Licensed User
Longtime User
hello erel.

you are my god.

i will use that silly 5 seconds trick. damn samsung!

i will just check if the p.Model() is i9300/i9305 and will do that only for sgs3.

for anyone else, i post the code that now works, with the 5 seconds delay

next time i will use the export feature

thanks a lot again

my best regards

ppp

B4X:
#Region  Project Attributes 
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim r2 As Reflector
   Dim p As Phone
   Dim TTS1 As TTS
   Dim pevents As PhoneEvents
   Dim dafare As Boolean
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim EditText1 As EditText
   Dim EditText2 As EditText
   Dim Button1 As Button
   Dim Button2 As Button
   Dim Button3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")

   Activity.LoadLayout("main")
    TTS1.Initialize("TTS1")
   pevents.Initialize("pEvents")
   dafare = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pEvents_TextToSpeechFinish (Intent As Intent)
   Dim tel As PhoneCalls
   Dim telefonata As Intent
   
   r2.Target = r2.GetContext
    r2.Target = r2.RunMethod2("getSystemService", "audio", "java.lang.String")
    r2.RunMethod2("setMode", 0, "java.lang.int")
    If dafare = False Then
      dafare = True
      Return
   End If
   Sleep(5000)
   telefonata = tel.Call(EditText1.Text)
   StartActivity(telefonata)
End Sub

Sub Button1_Click
   TTS1.SetLanguage(EditText2.Text,"")
    'r2.Target = r2.GetContext
    'r2.Target = r2.RunMethod2("getSystemService", "audio", "java.lang.String")
    'r2.RunMethod2("setMode", 2, "java.lang.int")
   TTS1.Speak("Hello. testing. 1. 2. 3. phone.", False)
End Sub

Sub Button2_Click
   TTS1.SetLanguage(EditText2.Text,"")
    r2.Target = r2.GetContext
    r2.Target = r2.RunMethod2("getSystemService", "audio", "java.lang.String")
    r2.RunMethod2("setMode", 2, "java.lang.int")
   TTS1.Speak("Hello. testing. 1. 2. 3. phone.", False)
End Sub

Sub Button3_Click
   dafare = False
   TTS1.Stop
End Sub

Sub Sleep(ticks As Int)
    Dim Lock1 As Lock
    Dim milliseconds As Int

    DoEvents
    If ticks > 0 AND DateTime.TicksPerSecond > 0 Then
        milliseconds = 1000 / DateTime.TicksPerSecond * ticks
        Lock1.Initialize(True)
        Lock1.WaitFor(milliseconds)
        Lock1.Unlock
    End If
End Sub
 
Upvote 0
Top