I'm making a simple copy of Google's TTS. All the code is working and have 2 buttons, one downloads the audio-clip and the second should let you listen to the audio before downloading.
The listen button does what it should and plays the audio on Mozilla Firefox. All other browsers just open a new tab and their built-in media player is displayed, but don't actually play the clip.
Here is the PHP code for the Listen button:
<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
/** Retrieve values from html form **/
$text = $_POST['textinput'];
$language = $_POST['language'];
$voicename = $_POST['voicename'];
$pitch = $_POST['pitchInput'];
$speed = $_POST['speedInput'];
// create client object
$client = new TextToSpeechClient();
$input_text = (new SynthesisInput())
->setText($text);
// voice and audio configuration
$voice = (new VoiceSelectionParams())
->setLanguageCode($language)
->setName($voicename)
->setSsmlGender(SsmlVoiceGender::FEMALE);
$audioConfig = (new AudioConfig())
->setPitch($pitch)
->setSpeakingRate($speed)
->setAudioEncoding(AudioEncoding::MP3);
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
$audioContent = $response->getAudioContent();
file_put_contents('output.mp3', $audioContent);
print('Audio content written to "output.mp3"' . PHP_EOL);
header('Content-Type: audio/mpeg');
header('Content-Disposition: inline; filename= "output.mp3" ');
readfile('output.mp3',$audioContent);
$client->close();
?>