0

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();
?>
UzZzi
  • 55
  • 1
  • 7
  • Any errors in the console of that window in those browsers? Are you able to press play and play the audio? Also how are you triggering this PHP from the browser exactly? All that is potentially more relevant than most of this PHP. Did you consider using a HTML5 audio tag? – ADyson Apr 06 '21 at 16:44
  • 1
    So what you are looking for is a media player on a webpage? Did I understand that correctly? Instead of pushing the .mp3 file to the browser and hope that the browser has a registered device for it why not just use a media player instead? – hppycoder Apr 06 '21 at 16:45
  • Look at what the ` – hppycoder Apr 06 '21 at 16:53
  • @ADyson no errors. I'm not, the play button is greyed out. In my html code i have a that runs this script once it's clicked. I haven't looked too in depth into it, but wouldn't that just play the audio? I would need to run the script somehow first so that it generates the file it's going to stream. – UzZzi Apr 06 '21 at 17:09
  • @hppycoder looks like that's the way to go. I'll try that – UzZzi Apr 06 '21 at 17:10
  • `wouldn't that just play the audio`...depends on what the browser supports. Which browsers specifically have you tested on? And what versions? https://caniuse.com/mp3 suggests support is common in recent versions of most common browsers, though. – ADyson Apr 06 '21 at 17:38
  • All modern browsers should have native support for playing audio files. See this thread for ideas: https://stackoverflow.com/questions/10105063/how-to-play-a-notification-sound-on-websites – SJacks Apr 06 '21 at 22:14

0 Answers0