Why is the package missing 'AudioEncoding'?

213 Views Asked by At

I'm trying to create an Text To Speech service which uses Google API Text-To-Speech. Hee is my code:

// Setup Google Client
require_once '../../plugins/php/google-api-php-client-2.4.1/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('../../plugins/php/google-api-php-client-2.4.1/credentials.json');
$client->addScope(Google_Service_Texttospeech::CLOUD_PLATFORM);
$service = new Google_Service_Texttospeech($client);

// Setup Request
$input = new Google_Service_Texttospeech_SynthesisInput();
$input->setText('Japan\'s national soccer team won against Colombia!');

$voice = new Google_Service_Texttospeech_VoiceSelectionParams();
$voice->setLanguageCode('en-US');

$audioConfig = new Google_Service_Texttospeech_AudioConfig();
$audioConfig->setAudioEncoding(Google_Service_Texttospeech_AudioEncoding::MP3);

Everything works except the last line which is giving the error:

Fatal error: Uncaught Error: Class 'AudioEncoding' not found in /home/******/public_html/services/remotes/post/convert-text-to-speech.php:28 Stack trace: #0 {main} thrown in /home/******/public_html/services/remotes/post/convert-text-to-speech.php on line 28

Any help on this?

1

There are 1 best solutions below

0
On BEST ANSWER

You appear to be using the Google Apis PHP client library which enables you to work with Google APIs such as Gmail, Drive or YouTube on your server.

You should be using the Google Cloud PHP client library This client supports accessing Google Cloud Platform services.

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

$textToSpeechClient = new TextToSpeechClient();

$input = new SynthesisInput();
$input->setText('Japan\'s national soccer team won against Colombia!');
$voice = new VoiceSelectionParams();
$voice->setLanguageCode('en-US');
$audioConfig = new AudioConfig();
$audioConfig->setAudioEncoding(AudioEncoding::MP3);

$resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig);
file_put_contents('test.mp3', $resp->getAudioContent());