B4J Question TTS Text-to-Speech for B4J ?

GMan

Well-Known Member
Licensed User
Longtime User
Is there - as in B4A - a library for TTS for B4J or can i - somehow - use an external TTS-Library ?
 

hatzisn

Expert
Licensed User
Longtime User
You can download texttospeech content from Google Translate for texts up to 160 characters. Use this code.

B4X:
Sub Button1_Click
    DownloadTTSSpeak("Καλημέρα, τι κάνεις; Όλα καλά;", "el", "KalimeraTiKaneis")
End Sub


Sub DownloadTTSSpeak(TextToSpeak As String, Language As String, FilenameToSaveWithOutExtension As String)
    Dim hj As HttpJob
    hj.Initialize("", Me)
    hj.Download($"https://translate.google.com/translate_tts?ie=UTF-&&client=tw-ob&tl=${Language}&q=${TextToSpeak};"$)
    Wait For (hj) JobDone(hj As HttpJob)
    If hj.Success Then
        File.WriteBytes("C:\Del", $"${FilenameToSaveWithOutExtension}.mp3"$, Bit.InputStreamToBytes(hj.GetInputStream))
        xui.MsgboxAsync("Ok World!", "B4X")
    Else
        xui.MsgboxAsync("Not Ok World!", "B4X")
    End If
End Sub
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
And another alternative without saving the file:

B4X:
Sub Button1_Click
    DownloadTTSSpeak2("Καλημέρα, τι κάνεις; Όλα καλά;", "el")
End Sub

Sub DownloadTTSSpeak2(TextToSpeak As String, Language As String)
    Dim joMedia As JavaObject
    Dim su As StringUtils
    Dim sURL As String = $"https://translate.google.com/translate_tts?ie=UTF-&&client=tw-ob&tl=${Language}&q=${su.EncodeUrl(TextToSpeak, "UTF8")};"$
    Log(sURL)
    joMedia.InitializeNewInstance("javafx.scene.media.Media", Array As Object(sURL))
    Dim joMp As JavaObject
    joMp.InitializeNewInstance("javafx.scene.media.MediaPlayer", Array As Object(joMedia))
    joMp.RunMethod("play", Null)
End Sub

Although If the TTS involves speaking the same text repeatedly I would prefer to download the files and create a map with the text to speak as key and the filename as value. Then it is a piece of cake to make it say the same things.
 
Last edited:
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
You can download texttospeech content from Google Translate for texts up to 160 characters. Use this code.

B4X:
Sub Button1_Click
    DownloadTTSSpeak("Καλημέρα, τι κάνεις; Όλα καλά;", "el", "KalimeraTiKaneis")
End Sub


Sub DownloadTTSSpeak(TextToSpeak As String, Language As String, FilenameToSaveWithOutExtension As String)
    Dim hj As HttpJob
    hj.Initialize("", Me)
    hj.Download($"https://translate.google.com/translate_tts?ie=UTF-&&client=tw-ob&tl=${Language}&q=${TextToSpeak};"$)
    Wait For (hj) JobDone(hj As HttpJob)
    If hj.Success Then
        File.WriteBytes("C:\Del", $"${FilenameToSaveWithOutExtension}.mp3"$, Bit.InputStreamToBytes(hj.GetInputStream))
        xui.MsgboxAsync("Ok World!", "B4X")
    Else
        xui.MsgboxAsync("Not Ok World!", "B4X")
    End If
End Sub
Thx - this sample works
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
And another alternative without saving the file:

B4X:
Sub Button1_Click
    DownloadTTSSpeak2("Καλημέρα, τι κάνεις; Όλα καλά;", "el")
End Sub

Sub DownloadTTSSpeak2(TextToSpeak As String, Language As String)
    Dim joMedia As JavaObject
    Dim su As StringUtils
    Dim sURL As String = $"https://translate.google.com/translate_tts?ie=UTF-&&client=tw-ob&tl=${Language}&q=${su.EncodeUrl(TextToSpeak, "UTF8")};"$
    Log(sURL)
    joMedia.InitializeNewInstance("javafx.scene.media.Media", Array As Object(sURL))
    Dim joMp As JavaObject
    joMp.InitializeNewInstance("javafx.scene.media.MediaPlayer", Array As Object(joMedia))
    joMp.RunMethod("play", Null)
End Sub

Although If the TTS involves speaking the same text repeatedly I would prefer to download the files and create a map with the text to speak as key and the filename as value. Then it is a piece of cake to make it say the same things.
This dont do anything...i think a player should play the file, but nothing to see or to hear
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
This dont do anything...i think a player should play the file, but nothing to see or to hear

Does it break? It works for me. What version of Java are you using?
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
You should try using Java jdk-14.0.1. Also if you want to save the files it might be better to include them all in your installation file or download a zip from your site.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
You should try using Java jdk-14.0.1. Also if you want to save the files it might be better to include them all in your installation file or download a zip from your site.
I am using the jdk1.8.0_66 (i think)
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Upvote 0
Top