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
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!!!

I receive HTML code telling me that access to this page is not given.
Any Idea how this could be done??

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"

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

--2022-01-24 13:34:04-- https://translate.google.com/translate_tts?ie=UTF-8&tl=de-TR&client=tw-ob&q=Guten Morgen
Resolving translate.google.com (translate.google.com)... 2404:6800:4015:803::200e, 142.250.70.206
Connecting to translate.google.com (translate.google.com)|2404:6800:4015:803::200e|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [audio/mpeg]
Saving to: 'translate_tts@ie=UTF-8&tl=de-TR&client=tw-ob&q=Guten Morgen'

translate_tts@ie=UTF-8&tl=de-TR&client=tw-ob&q=Gu [ <=> ] 6.09K --.-KB/s in 0.06s

2022-01-24 13:34:05 (105 KB/s) - 'translate_tts@ie=UTF-8&tl=de-TR&client=tw-ob&q=Guten Morgen' saved [6240]

D:\wget> dir

24/01/2022 01:34 PM 6,240 translate_tts@ie=UTF-8&tl=de-TR&client=tw-ob&q=Guten Morgen

D:\wget>
rename trans*gen GutenMorgen.mp3
 

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
Top