0

我正在尝试创建一个使用 Google API Text-To-Speech 的 Text To Speech 服务。嘻嘻是我的代码:

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

对此有什么帮助吗?

4

1 回答 1

0

您似乎正在使用Google APIs PHP 客户端库,它使您能够在您的服务器上使用 Google API,例如 Gmail、Drive 或 YouTube。

您应该使用Google Cloud PHP 客户端库此客户端支持访问 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());
于 2020-05-11T06:27:00.100 回答