List available voices
Stay organized with collections
Save and categorize content based on your preferences.
Shows how to list the available voices.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# List available voices\n\nShows how to list the available voices.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Supported voices and languages](/text-to-speech/docs/list-voices-and-types)\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Text-to-Speech, see\n[Text-to-Speech client libraries](/text-to-speech/docs/libraries).\n\n\nFor more information, see the\n[Text-to-Speech Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/texttospeech/latest/apiv1).\n\n\nTo authenticate to Text-to-Speech, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // ListVoices lists the available text to speech voices.\n func ListVoices(w io.Writer) error {\n \tctx := context.Background()\n\n \tclient, err := texttospeech.NewClient(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer client.Close()\n\n \t// Performs the list voices request.\n \tresp, err := client.ListVoices(ctx, &texttospeechpb.ListVoicesRequest{})\n \tif err != nil {\n \t\treturn err\n \t}\n\n \tfor _, voice := range resp.Voices {\n \t\t// Display the voice's name. Example: tpc-vocoded\n \t\tfmt.Fprintf(w, \"Name: %v\\n\", voice.Name)\n\n \t\t// Display the supported language codes for this voice. Example: \"en-US\"\n \t\tfor _, languageCode := range voice.LanguageCodes {\n \t\t\tfmt.Fprintf(w, \" Supported language: %v\\n\", languageCode)\n \t\t}\n\n \t\t// Display the SSML Voice Gender.\n \t\tfmt.Fprintf(w, \" SSML Voice Gender: %v\\n\", voice.SsmlGender.String())\n\n \t\t// Display the natural sample rate hertz for this voice. Example: 24000\n \t\tfmt.Fprintf(w, \" Natural Sample Rate Hertz: %v\\n\",\n \t\t\tvoice.NaturalSampleRateHertz)\n \t}\n\n \treturn nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Text-to-Speech, see\n[Text-to-Speech client libraries](/text-to-speech/docs/libraries).\n\n\nFor more information, see the\n[Text-to-Speech Java API\nreference documentation](/java/docs/reference/google-cloud-texttospeech/latest/overview).\n\n\nTo authenticate to Text-to-Speech, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * Demonstrates using the Text to Speech client to list the client's supported voices.\n *\n * @throws Exception on TextToSpeechClient Errors.\n */\n public static List\u003cVoice\u003e listAllSupportedVoices() throws Exception {\n // Instantiates a client\n try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {\n // Builds the text to speech list voices request\n ListVoicesRequest request = ListVoicesRequest.getDefaultInstance();\n\n // Performs the list voices request\n ListVoicesResponse response = textToSpeechClient.listVoices(request);\n List\u003cVoice\u003e voices = response.getVoicesList();\n\n for (Voice voice : voices) {\n // Display the voice's name. Example: tpc-vocoded\n System.out.format(\"Name: %s\\n\", voice.getName());\n\n // Display the supported language codes for this voice. Example: \"en-us\"\n List\u003cByteString\u003e languageCodes = voice.getLanguageCodesList().asByteStringList();\n for (ByteString languageCode : languageCodes) {\n System.out.format(\"Supported Language: %s\\n\", languageCode.toStringUtf8());\n }\n\n // Display the SSML Voice Gender\n System.out.format(\"SSML Voice Gender: %s\\n\", voice.getSsmlGender());\n\n // Display the natural sample rate hertz for this voice. Example: 24000\n System.out.format(\"Natural Sample Rate Hertz: %s\\n\\n\", voice.getNaturalSampleRateHertz());\n }\n return voices;\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Text-to-Speech, see\n[Text-to-Speech client libraries](/text-to-speech/docs/libraries).\n\n\nFor more information, see the\n[Text-to-Speech Node.js API\nreference documentation](/nodejs/docs/reference/text-to-speech/latest).\n\n\nTo authenticate to Text-to-Speech, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const textToSpeech = require('https://cloud.google.com/nodejs/docs/reference/text-to-speech/latest/overview.html');\n\n const client = new textToSpeech.https://cloud.google.com/nodejs/docs/reference/text-to-speech/latest/overview.html();\n\n const [result] = await client.listVoices({});\n const voices = https://cloud.google.com/nodejs/docs/reference/text-to-speech/latest/text-to-speech/protos.google.longrunning.operation.html.voices;\n\n console.log('Voices:');\n voices.forEach(voice =\u003e {\n console.log(`Name: ${voice.name}`);\n console.log(` SSML Voice Gender: ${voice.ssmlGender}`);\n console.log(` Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`);\n console.log(' Supported languages:');\n voice.languageCodes.forEach(languageCode =\u003e {\n console.log(` ${languageCode}`);\n });\n });\n\n### PHP\n\n\nTo learn how to install and use the client library for Text-to-Speech, see\n[Text-to-Speech client libraries](/text-to-speech/docs/libraries).\n\n\nTo authenticate to Text-to-Speech, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\TextToSpeech\\V1\\Client\\TextToSpeechClient;\n use Google\\Cloud\\TextToSpeech\\V1\\ListVoicesRequest;\n\n function list_voices(): void\n {\n // create client object\n $client = new TextToSpeechClient();\n\n // perform list voices request\n $request = (new ListVoicesRequest());\n $response = $client-\u003elistVoices($request);\n $voices = $response-\u003egetVoices();\n\n foreach ($voices as $voice) {\n // display the voice's name. example: tpc-vocoded\n printf('Name: %s' . PHP_EOL, $voice-\u003egetName());\n\n // display the supported language codes for this voice. example: 'en-US'\n foreach ($voice-\u003egetLanguageCodes() as $languageCode) {\n printf('Supported language: %s' . PHP_EOL, $languageCode);\n }\n\n // SSML voice gender values from TextToSpeech\\V1\\SsmlVoiceGender\n $ssmlVoiceGender = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE', 'FEMALE',\n 'NEUTRAL'];\n\n // display the SSML voice gender\n $gender = $voice-\u003egetSsmlGender();\n printf('SSML voice gender: %s' . PHP_EOL, $ssmlVoiceGender[$gender]);\n\n // display the natural hertz rate for this voice\n printf('Natural Sample Rate Hertz: %d' . PHP_EOL,\n $voice-\u003egetNaturalSampleRateHertz());\n }\n\n $client-\u003eclose();\n }\n\n### Python\n\n\nTo learn how to install and use the client library for Text-to-Speech, see\n[Text-to-Speech client libraries](/text-to-speech/docs/libraries).\n\n\nFor more information, see the\n[Text-to-Speech Python API\nreference documentation](/python/docs/reference/texttospeech/latest).\n\n\nTo authenticate to Text-to-Speech, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def list_voices():\n \"\"\"Lists the available voices.\"\"\"\n from google.cloud import texttospeech\n\n client = texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.services.text_to_speech.TextToSpeechClient.html()\n\n # Performs the list voices request\n voices = client.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.services.text_to_speech.TextToSpeechClient.html#google_cloud_texttospeech_v1_services_text_to_speech_TextToSpeechClient_list_voices()\n\n for voice in voices.voices:\n # Display the voice's name. Example: tpc-vocoded\n print(f\"Name: {voice.name}\")\n\n # Display the supported language codes for this voice. Example: \"en-US\"\n for language_code in voice.language_codes:\n print(f\"Supported language: {language_code}\")\n\n ssml_gender = texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.types.SsmlVoiceGender.html(voice.ssml_gender)\n\n # Display the SSML Voice Gender\n print(f\"SSML Voice Gender: {ssml_gender.name}\")\n\n # Display the natural sample rate hertz for this voice. Example: 24000\n print(f\"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\\n\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=tts)."]]