Alle unterstützten Stimmen auflisten

Sie können eine vollständige Liste aller unterstützten Stimmen erhalten, wenn Sie den Endpunkt voices:list der API aufrufen. Außerdem finden Sie die vollständige Liste der verfügbaren Stimmen auf der Seite Unterstützte Stimmen.

Die folgenden Code-Snippets zeigen, wie Sie die in der Text-to-Speech API verfügbaren Stimmen für die synthetische Sprachausgabe auflisten.

Für diese Beispiele muss gcloud eingerichtet und ein Dienstkonto erstellt oder aktiviert sein. Informationen zum Einrichten von gcloud sowie zum Erstellen und Auswählen eines Dienstkontos finden Sie unter Kurzanleitung: Text-to-Speech.

Protokoll

Ausführliche Informationen finden Sie in der Referenz zum API-Endpunkt voices:list.

Sie können eine Liste der verfügbaren Stimmen für die synthetische Sprachausgabe mit einer GET-Anfrage an den API-Endpunkt voices:list abrufen.

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

Die Text-to-Speech API gibt ein JSON-formatiertes Ergebnis zurück, das in etwa so aussieht:

{
  "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


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

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

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

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")

Weitere Sprachen

C#: Folgen Sie der Anleitung zur Einrichtung von C# auf der Seite "Clientbibliotheken" und rufen Sie dann die Text-to-Speech-Referenzdokumentation für .NET auf.

PHP: Folgen Sie der Anleitung zur Einrichtung von PHP auf der Seite "Clientbibliotheken" und rufen Sie dann die Text-to-Speech-Referenzdokumentation für PHP auf.

Ruby: Folgen Sie der Anleitung zur Einrichtung von Ruby auf der Seite "Clientbibliotheken" und rufen Sie dann die Text-to-Speech-Referenzdokumentation für Ruby auf.