使用 SDK for Rust 的 Amazon Polly 範例 - 適用於 Rust 的 AWS SDK

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK for Rust 的 Amazon Polly 範例

下列程式碼範例示範如何使用 AWS SDK for Rust 搭配 Amazon Polly 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

案例是向您展示如何呼叫服務中的多個函數或與其他 AWS 服務組合來完成特定任務的程式碼範例。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

動作

下列程式碼範例示範如何使用 DescribeVoices

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 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(()) }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 DescribeVoices

下列程式碼範例示範如何使用 ListLexicons

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

async fn show_lexicons(client: &Client) -> Result<(), Error> { let resp = client.list_lexicons().send().await?; println!("Lexicons:"); let lexicons = resp.lexicons(); for lexicon in lexicons { println!(" Name: {}", lexicon.name().unwrap_or_default()); println!( " Language: {:?}\n", lexicon .attributes() .as_ref() .map(|attrib| attrib .language_code .as_ref() .expect("languages must have language codes")) .expect("languages must have attributes") ); } println!(); println!("Found {} lexicons.", lexicons.len()); println!(); Ok(()) }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 ListLexicons

下列程式碼範例示範如何使用 PutLexicon

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

async fn make_lexicon(client: &Client, name: &str, from: &str, to: &str) -> Result<(), Error> { let content = format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <lexicon version=\"1.0\" xmlns=\"http://www.w3.org/2005/01/pronunciation-lexicon\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3.org/2005/01/pronunciation-lexicon http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd\" alphabet=\"ipa\" xml:lang=\"en-US\"> <lexeme><grapheme>{}</grapheme><alias>{}</alias></lexeme> </lexicon>", from, to); client .put_lexicon() .name(name) .content(content) .send() .await?; println!("Added lexicon"); Ok(()) }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 PutLexicon

下列程式碼範例示範如何使用 SynthesizeSpeech

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

async fn synthesize(client: &Client, filename: &str) -> Result<(), Error> { let content = fs::read_to_string(filename); let resp = client .synthesize_speech() .output_format(OutputFormat::Mp3) .text(content.unwrap()) .voice_id(VoiceId::Joanna) .send() .await?; // Get MP3 data from response and save it let mut blob = resp .audio_stream .collect() .await .expect("failed to read data"); let parts: Vec<&str> = filename.split('.').collect(); let out_file = format!("{}{}", String::from(parts[0]), ".mp3"); let mut file = tokio::fs::File::create(out_file) .await .expect("failed to create file"); file.write_all_buf(&mut blob) .await .expect("failed to write to file"); Ok(()) }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 SynthesizeSpeech

案例

以下程式碼範例顯示做法:

  • 使用 Amazon Polly 將純文字 (UTF-8) 輸入檔案合成至音訊檔案中。

  • 將音訊檔案上傳至 Amazon S3 儲存貯體。

  • 使用 Amazon Transcribe 將音訊檔案轉換為文字。

  • 顯示文字。

SDK for Rust

使用 Amazon Polly 將純文字 (UTF-8) 輸入檔案合成至音訊檔案中,將音訊檔案上傳至 Amazon S3 儲存貯體,使用 Amazon Transcribe 將該音訊檔案轉換為文字,然後顯示文字。

如需完整的原始碼和如何設定及執行的指示,請參閱 GitHub 上的完整範例。

此範例中使用的服務
  • Amazon Polly

  • Amazon S3

  • Amazon Transcribe