Contoh Amazon Polly menggunakan Ruby SDK - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh Amazon Polly menggunakan Ruby SDK

Contoh kode berikut menunjukkan cara melakukan tindakan dan menerapkan skenario umum dengan menggunakan Amazon Polly. AWS SDK for Ruby

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Meskipun tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks pada skenario terkait dan contoh lintas layanan.

Skenario adalah contoh kode yang menunjukkan cara menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan yang sama.

Setiap contoh menyertakan tautan ke GitHub, di mana Anda dapat menemukan petunjuk tentang cara mengatur dan menjalankan kode dalam konteks.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanDescribeVoices.

SDKuntuk Ruby
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

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 => ex puts "Could not get voices" puts "Error message:" puts ex.message end
  • Untuk API detailnya, lihat DescribeVoicesdi AWS SDK for Ruby APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanListLexicons.

SDKuntuk Ruby
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

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 resp = polly.list_lexicons resp.lexicons.each do |l| puts l.name puts " Alphabet:" + l.attributes.alphabet puts " Language:" + l.attributes.language puts end rescue StandardError => ex puts "Could not get lexicons" puts "Error message:" puts ex.message end
  • Untuk API detailnya, lihat ListLexiconsdi AWS SDK for Ruby APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanSynthesizeSpeech.

SDKuntuk Ruby
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

require "aws-sdk-polly" # In v2: require 'aws-sdk' begin # Get the filename from the command line if ARGV.empty? puts "You must supply a filename" exit 1 end filename = ARGV[0] # Open file and get the contents as a string if File.exist?(filename) contents = IO.read(filename) else puts "No such file: " + filename exit 1 end # 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 resp = polly.synthesize_speech({ output_format: "mp3", text: contents, voice_id: "Joanna", }) # Save output # Get just the file name # abc/xyz.txt -> xyx.txt name = File.basename(filename) # Split up name so we get just the xyz part parts = name.split(".") first_part = parts[0] mp3_file = first_part + ".mp3" IO.copy_stream(resp.audio_stream, mp3_file) puts "Wrote MP3 content to: " + mp3_file rescue StandardError => ex puts "Got error:" puts "Error message:" puts ex.message end