Há mais AWS SDK exemplos disponíveis no GitHub repositório AWS Doc SDK Examples
As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Use DescribeVoices
com um AWS SDK
Os exemplos de código a seguir mostram como usar o DescribeVoices
.
- .NET
-
- AWS SDK for .NET
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; public class DescribeVoices { public static async Task Main() { var client = new AmazonPollyClient(); var allVoicesRequest = new DescribeVoicesRequest(); var enUsVoicesRequest = new DescribeVoicesRequest() { LanguageCode = "en-US", }; try { string nextToken; do { var allVoicesResponse = await client.DescribeVoicesAsync(allVoicesRequest); nextToken = allVoicesResponse.NextToken; allVoicesRequest.NextToken = nextToken; Console.WriteLine("\nAll voices: "); allVoicesResponse.Voices.ForEach(voice => { DisplayVoiceInfo(voice); }); } while (nextToken is not null); do { var enUsVoicesResponse = await client.DescribeVoicesAsync(enUsVoicesRequest); nextToken = enUsVoicesResponse.NextToken; enUsVoicesRequest.NextToken = nextToken; Console.WriteLine("\nen-US voices: "); enUsVoicesResponse.Voices.ForEach(voice => { DisplayVoiceInfo(voice); }); } while (nextToken is not null); } catch (Exception ex) { Console.WriteLine("Exception caught: " + ex.Message); } } public static void DisplayVoiceInfo(Voice voice) { Console.WriteLine($" Name: {voice.Name}\tGender: {voice.Gender}\tLanguageName: {voice.LanguageName}"); } }
-
Para API obter detalhes, consulte DescribeVoicesem AWS SDK for .NET APIReferência.
-
- Java
-
- SDKpara Java 2.x
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.polly.PollyClient; import software.amazon.awssdk.services.polly.model.DescribeVoicesRequest; import software.amazon.awssdk.services.polly.model.DescribeVoicesResponse; import software.amazon.awssdk.services.polly.model.PollyException; import software.amazon.awssdk.services.polly.model.Voice; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeVoicesSample { public static void main(String args[]) { PollyClient polly = PollyClient.builder() .region(Region.US_WEST_2) .build(); describeVoice(polly); polly.close(); } public static void describeVoice(PollyClient polly) { try { DescribeVoicesRequest voicesRequest = DescribeVoicesRequest.builder() .languageCode("en-US") .build(); DescribeVoicesResponse enUsVoicesResult = polly.describeVoices(voicesRequest); List<Voice> voices = enUsVoicesResult.voices(); for (Voice myVoice : voices) { System.out.println("The ID of the voice is " + myVoice.id()); System.out.println("The gender of the voice is " + myVoice.gender()); } } catch (PollyException e) { System.err.println("Exception caught: " + e); System.exit(1); } } }
-
Para API obter detalhes, consulte DescribeVoicesem AWS SDK for Java 2.x APIReferência.
-
- Python
-
- SDKpara Python (Boto3)
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def describe_voices(self): """ Gets metadata about available voices. :return: The list of voice metadata. """ try: response = self.polly_client.describe_voices() self.voice_metadata = response["Voices"] logger.info("Got metadata about %s voices.", len(self.voice_metadata)) except ClientError: logger.exception("Couldn't get voice metadata.") raise else: return self.voice_metadata
-
Para API obter detalhes, consulte a DescribeVoicesReferência AWS SDK do Python (Boto3). API
-
- Ruby
-
- SDKpara Ruby
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. require 'aws-sdk-polly' # In v2: require 'aws-sdk' begin # Create an Amazon Polly client using # credentials from the shared credentials file ~/.aws/credentials # and the configuration (region) from the shared configuration file ~/.aws/config polly = Aws::Polly::Client.new # Get US English voices resp = polly.describe_voices(language_code: 'en-US') resp.voices.each do |v| puts v.name puts " #{v.gender}" puts end rescue StandardError => e puts 'Could not get voices' puts 'Error message:' puts e.message end
-
Para API obter detalhes, consulte DescribeVoicesem AWS SDK for Ruby APIReferência.
-
- Rust
-
- SDKpara Rust
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. async fn list_voices(client: &Client) -> Result<(), Error> { let resp = client.describe_voices().send().await?; println!("Voices:"); let voices = resp.voices(); for voice in voices { println!(" Name: {}", voice.name().unwrap_or("No name!")); println!( " Language: {}", voice.language_name().unwrap_or("No language!") ); println!(); } println!("Found {} voices", voices.len()); Ok(()) }
-
Para API obter detalhes, consulte DescribeVoices
a AWS SDKAPIreferência do Rust.
-