I am trying to convert words to speech ..
Untill now I have tried this:
<?php
$text = "Hello this is a test for voice api of google";
// Name of the MP3 file generated using the MD5 hash
$file = md5($text);
// Save the MP3 file in this folder with the .mp3 extension
$file = "audio/" . $file .".mp3";
if($file) {
echo "created";
} else {
echo "not created";
}
// If the MP3 file exists, do not create a new request
if (!file_exists($file)) {
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?q=' . $text);
echo "hello";
file_put_contents($file, $mp3);
} else {
echo "hii";
}
?>
In my html file :
<audio controls="controls" autoplay="autoplay">
<source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>
I am getting created hello and an audio player in output. But no file is played and neither it is created in the folder?
I found it:
Important:
client=tw-ob
YOUR_LANGUAGE_CODE
can be en,us,uk,vi etc.An improved version:
You cannot use this service for free.
Check translate API FAQ
More info about this unofficial way of use you can find on Techcrunch
Your file is not creating because you forgot to create it , use below code for creating the file.
There is a problem with the url you try to access. It is broken ! You should have tried first. The new URL, that I found on the FF console is :
http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&total=1&idx=0&textlen=5&prev=input
For the single word Hello. And you see that you have to specify the language, and the length of your text in textlen, even though it did work for all the sentences I tried without changing this var.
Another problem is that you have to urlencode() your text, or you will have a bug with accents and punctuation. So the line to download the MP3 becomes :
So the complete code looks like :
You can also use the simple code below. Just echo the code to get the result. In this code, there is no need to save a file or getting permission problems.