January 9, 2024 5:02 am

How to convert text to MP3 speech/Voice using Google API

Today I am going to give you a great article on text to voice conversion.  To convert text to voice I have used Google TTS (Text to Speech) it will create an mp3 file and play it its a very simple script you must try its written in PHP it can convert almost 50+ languages. This Class converts text to speech using Google text to speech API to transform text to mp3 file which will be downloaded and later used as eg. embed file.

google-text-to-voice

[wpdm_file id=23]DEMO

index.php

<?php
include 'PHP_Text2Speech.class.php'; 
    $t2s = new PHP_Text2Speech; 
?>
    <audio controls="controls" autoplay="autoplay"> 
       <source src="<?php echo $t2s->speak("Hello PHPGang");?>" type="audio/mp3" />    
    </audio>

Calling Google url to get mp3 file.

if (!file_exists($this->mp3File)) { 
            $this->download("http://translate.google.com/translate_tts?ie=UTF-8&q={$this->text}&tl={$this->lang}&total={$this->wordCount}&idx=0&textlen={$this->textLen}", $this->mp3File); 
        }

If you have any problem in using this code please write in comments and need your feedback.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

10 responses to “How to convert text to MP3 speech/Voice using Google API”

  1. Kirubanandan.K says:

    How to convert mp3 to text?

  2. kami says:

    this is great but would you like to tell me is this free for life time to use google api??

  3. Michael Cerasoli says:

    hi its michael cerasoli i’ve been a programmer since 1990 using windows and client server applications before 1990 using basic, and the Texas Instruments TRS-80. All these concepts have been around since then from 1995 to present things just started going down. But that’s ok, Microsoft’s 90 billion dollar industry certainly made up for it. And OFFICE is an incredibile product isn’t it? Man when windows came out I read every book the operating system came with about 15 manuals I read every one of them and never had to learn a thing since (with my mainframe, unix, db2, and c programming background of course). Anyway this is great and I thank you for all your wonderful time and support. 🙂

  4. Michael Cerasoli says:

    It’s always good to give out the completed code but hey let the programmer figure it out I don’t think anyone wants some “hack” to come in and steal the show am I right?

    I just like to see the completed product you know syntax was always my problem in college anyway are you able to display the finished product, with html tags and all?

  5. GAMERAS says:

    NOT ABLE TO DOWNLOAD EVEN I HAVE ALREADY SUBSCRIBE

  6. Cameron Smith says:

    Hi, the google translate text to speech has a character limit of 100. Can you post php code that will break longer text into chunks of 100 characters and join the mp3 files together in one mp3?

  7. egold555 says:

    Your download subscription service is broken. Please fix or provide download

  8. Shais Khan Gamaryani says:

    how can i use it its not working for me

  9. Nelson Prabu J says:

    can i have same thing without google api..
    Example: in linux — espeak – cmd –

  10. Felipe Alejandro Higuera Cifue says:

    i cant see the download button… please fix it and thanx 4all

Leave a Reply

Your email address will not be published. Required fields are marked *