Text to Speech with Amazon Polly and .NET

Want to talk with other .NET on AWS developers, ask questions, and share what you know? Join us on Slack!

Download full source code.

AWS artificial intelligence services like Amazon Polly (not to be confused with the resilience library I write a lot about ) are very easy to use, all you need is an AWS account. You don’t have to deploy any infrastructure, you don’t need complex permissions, you don’t need to worry about scaling, and you don’t have to secure the service. All you need is an AWS account with the permission “AmazonPollyReadOnlyAccess” - if you are using an account with “AdministratorAccess”, you already have this permission.

Amazon Polly is a text-to-speech service that allows you to send short and long-form text for conversion to speech. The service supports multiple languages and voices.

In this simple example, you will see how to convert text to speech, and play it on your computer.


An AWS account with the permission “AmazonPollyReadOnlyAccess”.

The Code

Create a new .NET console application.

dotnet new console -o SendSentencesToPolly

Add two NuGet packages to the project:

dotnet add package AWSSDK.Polly
dotnet add package NetCoreAudio

Open the Program.cs file and replace with -

using Amazon.Polly;
using Amazon.Polly.Model;
using NetCoreAudio;

var pollyClient = new AmazonPollyClient();
var request = new SynthesizeSpeechRequest
    OutputFormat = OutputFormat.Mp3,
    VoiceId = VoiceId.Matthew,
var player = new Player();

while (true)
    Console.WriteLine("Enter text (CTRL-C to exit):");
    var text = Console.ReadLine();
    request.Text = text;
    var response = await pollyClient.SynthesizeSpeechAsync(request);

    using (var fileStream = new FileStream("speech.mp3", FileMode.Create, FileAccess.Write))

    await player.Play("speech.mp3");

Run it using dotnet run and enter some text. You should hear the text spoken in the voice of Matthew.

On Linux, you might need to install an audio player. On Ubuntu, you can install mpg123 using sudo apt install mpg123.

That’s it. You are using AI to convert text to speech, and you did it all from a .NET application.

Download full source code.

comments powered by Disqus