为什么包缺少'AudioEncoding'?

问题描述 投票:0回答:1

我正在尝试创建一个使用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);

除最后一行显示错误外,其他所有方法都有效:

致命错误:未捕获的错误:在/home/******/public_html/services/remotes/post/convert-text-to-speech.php:28中找不到类'AudioEncoding':堆栈跟踪:#0 { main}放在第28行的/home/******/public_html/services/remotes/post/convert-text-to-speech.php中]

对此有任何帮助吗?

google-api google-api-php-client google-text-to-speech google-cloud-php-client
1个回答
0
投票
您似乎正在使用Google Apis PHP client库,该库使您可以在服务器上使用Google API,例如Gmail,Dri​​ve或YouTube。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.