列出所有支持的语音

您可以通过调用 API 的 voices:list 端点来获取所有受支持语音的完整列表。您还可以在支持的语音页面上找到前述列表。

以下代码段演示如何列出 Text-to-Speech API 中提供的可用于文本转语音合成的语音。

这些示例要求您已安装并初始化 Google Cloud CLI。如需了解如何设置 gcloud CLI,请参阅向 TTS 进行身份验证

协议

如需了解完整的详细信息,请参阅 voices:list API 端点。

要获取文本转语音合成的可用语音列表,请向 voices:list API 端点发出 GET 请求。 将 PROJECT_ID 替换为您的项目 ID。

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "x-goog-user-project: PROJECT_ID" \
    -H "Content-Type: application/json; charset=utf-8" \
    "https://texttospeech.googleapis.com/v1/voices"

Text-to-Speech API 返回与以下内容类似的 JSON 格式的结果:

{
  "voices": [
    {
      "languageCodes": [
        "es-ES"
      ],
      "name": "es-ES-Standard-A",
      "ssmlGender": "FEMALE",
      "naturalSampleRateHertz": 24000
    },
    {
      "languageCodes": [
        "ja-JP"
      ],
      "name": "ja-JP-Standard-A",
      "ssmlGender": "FEMALE",
      "naturalSampleRateHertz": 22050
    },
    {
      "languageCodes": [
        "pt-BR"
      ],
      "name": "pt-BR-Standard-A",
      "ssmlGender": "FEMALE",
      "naturalSampleRateHertz": 24000
    },
        ...
  ]
}

Go

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Go API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


// ListVoices lists the available text to speech voices.
func ListVoices(w io.Writer) error {
	ctx := context.Background()

	client, err := texttospeech.NewClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	// Performs the list voices request.
	resp, err := client.ListVoices(ctx, &texttospeechpb.ListVoicesRequest{})
	if err != nil {
		return err
	}

	for _, voice := range resp.Voices {
		// Display the voice's name. Example: tpc-vocoded
		fmt.Fprintf(w, "Name: %v\n", voice.Name)

		// Display the supported language codes for this voice. Example: "en-US"
		for _, languageCode := range voice.LanguageCodes {
			fmt.Fprintf(w, "  Supported language: %v\n", languageCode)
		}

		// Display the SSML Voice Gender.
		fmt.Fprintf(w, "  SSML Voice Gender: %v\n", voice.SsmlGender.String())

		// Display the natural sample rate hertz for this voice. Example: 24000
		fmt.Fprintf(w, "  Natural Sample Rate Hertz: %v\n",
			voice.NaturalSampleRateHertz)
	}

	return nil
}

Java

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Java API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * Demonstrates using the Text to Speech client to list the client's supported voices.
 *
 * @throws Exception on TextToSpeechClient Errors.
 */
public static List<Voice> listAllSupportedVoices() throws Exception {
  // Instantiates a client
  try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
    // Builds the text to speech list voices request
    ListVoicesRequest request = ListVoicesRequest.getDefaultInstance();

    // Performs the list voices request
    ListVoicesResponse response = textToSpeechClient.listVoices(request);
    List<Voice> voices = response.getVoicesList();

    for (Voice voice : voices) {
      // Display the voice's name. Example: tpc-vocoded
      System.out.format("Name: %s\n", voice.getName());

      // Display the supported language codes for this voice. Example: "en-us"
      List<ByteString> languageCodes = voice.getLanguageCodesList().asByteStringList();
      for (ByteString languageCode : languageCodes) {
        System.out.format("Supported Language: %s\n", languageCode.toStringUtf8());
      }

      // Display the SSML Voice Gender
      System.out.format("SSML Voice Gender: %s\n", voice.getSsmlGender());

      // Display the natural sample rate hertz for this voice. Example: 24000
      System.out.format("Natural Sample Rate Hertz: %s\n\n", voice.getNaturalSampleRateHertz());
    }
    return voices;
  }
}

Node.js

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Node.js API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

const textToSpeech = require('@google-cloud/text-to-speech');

const client = new textToSpeech.TextToSpeechClient();

const [result] = await client.listVoices({});
const voices = result.voices;

console.log('Voices:');
voices.forEach(voice => {
  console.log(`Name: ${voice.name}`);
  console.log(`  SSML Voice Gender: ${voice.ssmlGender}`);
  console.log(`  Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`);
  console.log('  Supported languages:');
  voice.languageCodes.forEach(languageCode => {
    console.log(`    ${languageCode}`);
  });
});

Python

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Python API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

def list_voices():
    """Lists the available voices."""
    from google.cloud import texttospeech

    client = texttospeech.TextToSpeechClient()

    # Performs the list voices request
    voices = client.list_voices()

    for voice in voices.voices:
        # Display the voice's name. Example: tpc-vocoded
        print(f"Name: {voice.name}")

        # Display the supported language codes for this voice. Example: "en-US"
        for language_code in voice.language_codes:
            print(f"Supported language: {language_code}")

        ssml_gender = texttospeech.SsmlVoiceGender(voice.ssml_gender)

        # Display the SSML Voice Gender
        print(f"SSML Voice Gender: {ssml_gender.name}")

        # Display the natural sample rate hertz for this voice. Example: 24000
        print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n")

其他语言

C#:请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 的 TText-to-Speech 参考文档

PHP:请按照客户端库页面上的 PHP 设置说明 操作,然后访问 PHP 的 Text-to-Speech 参考文档

Ruby:请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 的 Text-to-Speech 参考文档