Android Question [Solved] Force TTS via device loudspeaker

mw71

Active Member
Licensed User
Longtime User
I use TTS and the Beep function.
With the beep function, I can use Initialize2 to specify the channel via which this is output.
How can I do this with TTS?

The background is that I would also like to have the TTS output via the device loudspeaker (if the user wishes),
even if the device is connected to a radio (or other audio device) via Bluetooth, for example.
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
Upvote 0

mw71

Active Member
Licensed User
Longtime User
hi,

thanks for your answers.

first I tested a code from the AI (does not work, opens the camera, but I wanted to show it for the sake of completeness)
from KI:
Sub AudioManager_Set
    Dim AudioManager As JavaObject
    AudioManager.InitializeStatic("android.media.AudioManager")

    Dim params(2) As Object
    params(0) = AudioManager.GetField("STREAM_MUSIC")
    params(1) = AudioManager.GetField("AUDIOFOCUS_GAIN_TRANSIENT")

    Dim result As Int = AudioManager.RunMethod("requestAudioFocus", params)
    Log("Audio Manager: " & result)
End Sub

Sub AudioManager_Release
    Dim AudioManager As JavaObject
    AudioManager.InitializeStatic("android.media.AudioManager")
    AudioManager.RunMethod("abandonAudioFocus", Null)
End Sub

then I tested the code (from the link) from @MicroDrie, unfortunately this does not help (the app works, but the output is always via the BT device)

Finally, I tried the code from the link from @Erel.
This basically works, but the speech is output via the device speaker AND the BT device.
At least in the test environment (Samsung A54 and hi-fi system, target is then the same/different cell phone and car radio)
I have also "changed" the code a bit, it is in a sub and this in a code module.
B4X:
'part of the Main Code:
        Try
            If TTS1.IsInitialized = False Then
                TTS1.Initialize("TTS1")
        End If

        Dim sr As Float = ((Page_Setup.pm_Speed.SelectedValue-10)*0.05) + 1
        TTS1.SpeechRate = sr    'default = 1

        TTS_Service.Speek(sb_1.ToString,TTS1)
        Sleep(1000)

        TTS_Service.Speek(sb_2.ToString,TTS1)

'in the Code Moule
Sub Speek(Text As String, tts_Target As TTS)
    Dim p As Phone
    Dim jo As JavaObject
    jo.InitializeNewInstance("java.util.HashMap", Null)
    jo.RunMethod("put", Array("streamType", "" & p.VOLUME_ALARM))
    Dim jts As JavaObject = tts_Target
    jts.RunMethod("speak", Array(Text, 1, jo)) '0 = QUEUE_FLUSH, 1 = QUEUE_ADD

End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…