Hey, guys its been a while, sorry about that, I am here to share something interest I found recently how you can possibly convert text to speech in mp3 format. This service is powered by Amazon Web Service, the article was first published on www.w3schools.info.
It supports multiple languages across countries and variety of human voices. You can able to choose voices both male and female based on the country/region. It is awesome, right? Yeah.
I prefer Amazon Polly because it is easy to add speech to your video, presentation, or online training course. Amazon Polly can generate speech in 24 languages, making it easy to add voice to applications with a global audience. With Amazon Polly, you can read any content like RSS feed, news, or email, and store the speech in MP3 audio formats.
Download and Install Amazon Polly PHP SDK Client
To get started, you must download the zip file, unzip it into your project to a location of your choosing, and include the autoloader:
Final Code
$credentials = new \Aws\Credentials\Credentials($awsAccessKeyId, $awsSecretKey); $client = new \Aws\Polly\PollyClient([ 'version' => '2016-06-10', 'credentials' => $credentials, 'region' => 'us-east-1', ]); $result = $client->synthesizeSpeech([ 'OutputFormat' => 'mp3', 'Text' => $text, 'TextType' => 'text', 'VoiceId' => 'Emma', ]); $resultData = $result->get('AudioStream')->getContents(); $local_mp3_file = $filePath . $FileName; file_put_contents($local_mp3_file, $resultData); // write mp3 file content to file header('Content-Transfer-Encoding: binary'); header('Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3'); header('Content-length: ' . strlen($resultData)); header('Content-Disposition: attachment; filename=$local_mp3_file'); header('X-Pad: avoid browser bug'); header('Cache-Control: no-cache');
Hit me with a comment!