Other Google text-to-speech via HTTP

prokli

Active Member
Licensed User
Longtime User
There are some reasons why I cannot use the build-in TTS engine. So, I was looking for a Google Service doing this and found this URL:

https://translate.google.com/translate_tts?ie=UTF-8&tl=de-TR&client=tw-ob&q=Guten Morgen

If I put this line into my browser I can hear "Good Morning" in German Language. Nice!!!
So I tried to run the code next, but I neither know how to play this locally on my smartphone nor to create a mp3 file, which I could use to play it.
HTTP MP3:
Public Sub oTTS
    Dim poststr As String
    Dim job As HttpJob
    Dim res As String
    job.Initialize("",Me)
    poststr = "https://translate.google.com/translate_tts?ie=UTF-8&tl=de-TR&client=tw-ob&q=Guten Morgen"
    job.PostString(poststr, "")
    job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0")
    'job.GetRequest.SetHeader("Content-Type", "application/json")
    'job.GetRequest.SetContentType("application/json")
    'job.GetRequest.SetContentEncoding("text/plain")

    Wait For (job) JobDone(j As HttpJob)
    If j.Success Then
        res = job.GetString
    End If
End Sub

"j.Sucess" gets never True!
I receive HTML code telling me that access to this page is not given.
Any Idea how this could be done??
 
Last edited:

emexes

Expert
Licensed User

FWIW it I tried downloading it with WGET, and that URL returned the playable MP3 file (rather than html with the audio hidden behind a player widget) so it seems that your idea has a good chance of working... ?

edit: in the following quote section, it also seems that the forum software is changing "Guten%20Morgen" to "Guten Morgen"

 

Attachments

  • GutenMorgen.zip
    5.9 KB · Views: 169
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
this works fine for me:
B4X:
Dim url As String = "https://translate.google.com/translate_tts?ie=UTF-8&tl=de-TR&client=tw-ob&q=Guten%20Morgen"
Dim j As HttpJob
 
j.Initialize("", Me)
j.Download(url)

Wait For (j) Jobdone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "morgen.mp3", False)
        File.Copy2(J.GetInputStream, out)
        out.Close
    Else
    Log(j.ErrorMessage)       
End If
j.Release

here's your .mp3 downloaded with okhttputils2 (sorry, had to zip it). your job is a get, not a post.
 

Attachments

  • morgen.zip
    5.9 KB · Views: 182
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…