Convert text to MP3 speech using Google API

In this post, I am going to write about text to voice conversion. For this, I will Google’s TTS (Text to speech) API. It will translate your text to an mp3 file. And you can simply plat this mp3 file. It is worth to note that Google’s text to speech API supports more than 50 languages. So, you can try this in your local languages.
Use of the API is very simple. You just need to call the API via the simple get request.
http://translate.google.com/translate_tts?q=Hello+word
And this will send a MP3 file in responce
Get the MP3 content and save it in a file.
<?php $url = 'http://translate.google.com/translate_tts?q='. $words; $mp3 = file_get_contents($url); file_put_contents($file, $mp3); ?>
Now use audio tag to embed and play this audio file in the browser.
<audio controls="controls" autoplay="autoplay"> <source src="<? echo $file; ?>" type="audio/mp3" /> Your browser does not support the audio element. </audio>
I created a demo page where you can see this API working live.
Tags: Google API | PHP
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.