文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 的 Amazon Polly 範例 AWS SDK for .NET
下列程式碼範例說明如何搭配 Amazon Polly AWS SDK for .NET 使用 來執行動作和實作常見案例。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。
案例是程式碼範例,示範如何透過呼叫服務內的多個函數或與其他 結合,來完成特定任務 AWS 服務。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
動作
下列程式碼範例示範如何使用 DeleteLexicon
。
- AWS SDK for .NET
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; /// <summary> /// Deletes an existing Amazon Polly lexicon using the AWS SDK for .NET. /// </summary> public class DeleteLexicon { public static async Task Main() { string lexiconName = "SampleLexicon"; var client = new AmazonPollyClient(); var success = await DeletePollyLexiconAsync(client, lexiconName); if (success) { Console.WriteLine($"Successfully deleted {lexiconName}."); } else { Console.WriteLine($"Could not delete {lexiconName}."); } } /// <summary> /// Deletes the named Amazon Polly lexicon. /// </summary> /// <param name="client">The initialized Amazon Polly client object.</param> /// <param name="lexiconName">The name of the Amazon Polly lexicon to /// delete.</param> /// <returns>A Boolean value indicating the success of the operation.</returns> public static async Task<bool> DeletePollyLexiconAsync( AmazonPollyClient client, string lexiconName) { var deleteLexiconRequest = new DeleteLexiconRequest() { Name = lexiconName, }; var response = await client.DeleteLexiconAsync(deleteLexiconRequest); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } }
-
如需API詳細資訊,請參閱 參考 DeleteLexicon中的 。 AWS SDK for .NET API
-
下列程式碼範例示範如何使用 DescribeVoices
。
- AWS SDK for .NET
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 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}"); } }
-
如需API詳細資訊,請參閱 參考 DescribeVoices中的 。 AWS SDK for .NET API
-
下列程式碼範例示範如何使用 GetLexicon
。
- AWS SDK for .NET
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; /// <summary> /// Retrieves information about a specific Amazon Polly lexicon. /// </summary> public class GetLexicon { public static async Task Main(string[] args) { string lexiconName = "SampleLexicon"; var client = new AmazonPollyClient(); await GetPollyLexiconAsync(client, lexiconName); } public static async Task GetPollyLexiconAsync(AmazonPollyClient client, string lexiconName) { var getLexiconRequest = new GetLexiconRequest() { Name = lexiconName, }; try { var response = await client.GetLexiconAsync(getLexiconRequest); Console.WriteLine($"Lexicon:\n Name: {response.Lexicon.Name}"); Console.WriteLine($"Content: {response.Lexicon.Content}"); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } }
-
如需API詳細資訊,請參閱 參考 GetLexicon中的 。 AWS SDK for .NET API
-
下列程式碼範例示範如何使用 ListLexicons
。
- AWS SDK for .NET
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; /// <summary> /// Lists the Amazon Polly lexicons that have been defined. By default, /// lists the lexicons that are defined in the same AWS Region as the default /// user. To view Amazon Polly lexicons that are defined in a different AWS /// Region, supply it as a parameter to the Amazon Polly constructor. /// </summary> public class ListLexicons { public static async Task Main() { var client = new AmazonPollyClient(); var request = new ListLexiconsRequest(); try { Console.WriteLine("All voices: "); do { var response = await client.ListLexiconsAsync(request); request.NextToken = response.NextToken; response.Lexicons.ForEach(lexicon => { var attributes = lexicon.Attributes; Console.WriteLine($"Name: {lexicon.Name}"); Console.WriteLine($"\tAlphabet: {attributes.Alphabet}"); Console.WriteLine($"\tLanguageCode: {attributes.LanguageCode}"); Console.WriteLine($"\tLastModified: {attributes.LastModified}"); Console.WriteLine($"\tLexemesCount: {attributes.LexemesCount}"); Console.WriteLine($"\tLexiconArn: {attributes.LexiconArn}"); Console.WriteLine($"\tSize: {attributes.Size}"); }); } while (request.NextToken is not null); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } }
-
如需API詳細資訊,請參閱 參考 ListLexicons中的 。 AWS SDK for .NET API
-
下列程式碼範例示範如何使用 PutLexicon
。
- AWS SDK for .NET
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; /// <summary> /// Creates a new Amazon Polly lexicon using the AWS SDK for .NET. /// </summary> public class PutLexicon { public static async Task Main() { string lexiconContent = "<?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>test1</grapheme><alias>test2</alias></lexeme>" + "</lexicon>"; string lexiconName = "SampleLexicon"; var client = new AmazonPollyClient(); var putLexiconRequest = new PutLexiconRequest() { Name = lexiconName, Content = lexiconContent, }; try { var response = await client.PutLexiconAsync(putLexiconRequest); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully created Lexicon: {lexiconName}."); } else { Console.WriteLine($"Could not create Lexicon: {lexiconName}."); } } catch (Exception ex) { Console.WriteLine("Exception caught: " + ex.Message); } } }
-
如需API詳細資訊,請參閱 參考 PutLexicon中的 。 AWS SDK for .NET API
-
下列程式碼範例示範如何使用 SynthesizeSpeech
。
- AWS SDK for .NET
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 using System; using System.IO; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; public class SynthesizeSpeech { public static async Task Main() { string outputFileName = "speech.mp3"; string text = "Twas brillig, and the slithy toves did gyre and gimbol in the wabe"; var client = new AmazonPollyClient(); var response = await PollySynthesizeSpeech(client, text); WriteSpeechToStream(response.AudioStream, outputFileName); } /// <summary> /// Calls the Amazon Polly SynthesizeSpeechAsync method to convert text /// to speech. /// </summary> /// <param name="client">The Amazon Polly client object used to connect /// to the Amazon Polly service.</param> /// <param name="text">The text to convert to speech.</param> /// <returns>A SynthesizeSpeechResponse object that includes an AudioStream /// object with the converted text.</returns> private static async Task<SynthesizeSpeechResponse> PollySynthesizeSpeech(IAmazonPolly client, string text) { var synthesizeSpeechRequest = new SynthesizeSpeechRequest() { OutputFormat = OutputFormat.Mp3, VoiceId = VoiceId.Joanna, Text = text, }; var synthesizeSpeechResponse = await client.SynthesizeSpeechAsync(synthesizeSpeechRequest); return synthesizeSpeechResponse; } /// <summary> /// Writes the AudioStream returned from the call to /// SynthesizeSpeechAsync to a file in MP3 format. /// </summary> /// <param name="audioStream">The AudioStream returned from the /// call to the SynthesizeSpeechAsync method.</param> /// <param name="outputFileName">The full path to the file in which to /// save the audio stream.</param> private static void WriteSpeechToStream(Stream audioStream, string outputFileName) { var outputStream = new FileStream( outputFileName, FileMode.Create, FileAccess.Write); byte[] buffer = new byte[2 * 1024]; int readBytes; while ((readBytes = audioStream.Read(buffer, 0, 2 * 1024)) > 0) { outputStream.Write(buffer, 0, readBytes); } // Flushes the buffer to avoid losing the last second or so of // the synthesized text. outputStream.Flush(); Console.WriteLine($"Saved {outputFileName} to disk."); } }
使用語音標記與使用 的 Amazon Polly 來合成文字的語音 AWS SDK。
using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; public class SynthesizeSpeechMarks { public static async Task Main() { var client = new AmazonPollyClient(); string outputFileName = "speechMarks.json"; var synthesizeSpeechRequest = new SynthesizeSpeechRequest() { OutputFormat = OutputFormat.Json, SpeechMarkTypes = new List<string> { SpeechMarkType.Viseme, SpeechMarkType.Word, }, VoiceId = VoiceId.Joanna, Text = "This is a sample text to be synthesized.", }; try { using (var outputStream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write)) { var synthesizeSpeechResponse = await client.SynthesizeSpeechAsync(synthesizeSpeechRequest); var buffer = new byte[2 * 1024]; int readBytes; var inputStream = synthesizeSpeechResponse.AudioStream; while ((readBytes = inputStream.Read(buffer, 0, 2 * 1024)) > 0) { outputStream.Write(buffer, 0, readBytes); } } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } }
-
如需API詳細資訊,請參閱 參考 SynthesizeSpeech中的 。 AWS SDK for .NET API
-
案例
下列程式碼範例會示範如何建立可分析客戶評論卡、從其原始語言進行翻譯、判斷對方情緒,以及透過翻譯後的文字產生音訊檔案的應用程式。
- AWS SDK for .NET
-
此範例應用程式會分析和存儲客戶的意見回饋卡。具體來說,它滿足了紐約市一家虛構飯店的需求。飯店以實體評論卡的形式收到賓客以各種語言撰寫的意見回饋。這些意見回饋透過 Web 用戶端上傳至應用程式。評論卡的影像上傳後,系統會執行下列步驟:
-
文字內容是使用 Amazon Textract 從影像中擷取。
-
Amazon Comprehend 會決定擷取文字及其用語的情感。
-
擷取的文字內容會使用 Amazon Translate 翻譯成英文。
-
Amazon Polly 會使用擷取的文字內容合成音訊檔案。
完整的應用程式可透過 AWS CDK 部署。如需原始程式碼和部署指示,請參閱中的 GitHub
專案。 此範例中使用的服務
Amazon Comprehend
Lambda
Amazon Polly
Amazon Textract
Amazon Translate
-