为什么包裹丢失 'AudioEncoding'?

Why is the package missing 'AudioEncoding'?

我正在尝试创建使用 Google API 文本转语音的文本转语音服务。 嘻嘻是我的代码:

// 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);

除最后一行出现错误外,一切正常:

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

对此有任何帮助吗?

您似乎在使用 Google Apis PHP client 库,它使您能够在服务器上使用 Google API,例如 Gmail、Drive 或 YouTube。

您应该使用 Google Cloud PHP client library 此客户端支持访问 Google 云平台服务。

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());