Instruction
This article demonstrates how to create a Text to Speech application using C# and XAML in Xamarin.Forms.
Text To Speech - Basic Usage
With this app, you can easily convert a text into speech (TTS). Just enter the text and the app speaks it for you.
Let’s start.
Step 1
Open Visual Studio and go to New Project >>Installed >> Visual C# >> Cross Platform.
Select Cross-Platform App, then give your project a name and location.
Step 2
After project creation, add the following Nuget Packages to your project.
• XXam.Plugins.TextToSpeech
Go to Solution Explorer and select your Solution. Right-click and select "Manage NuGet Packages for Solution".
Now, select the following Nuget Package and select your project to install it.
Step 3
Open Solution Explorer >> Project Name >> MainPage.xaml. Open the Design View of this page.
The code is given below.
Xaml Code
- <StackLayout>
- <Entry x:Name="Enter"/>
-
- <Button Text="Speek" Clicked="Clicked Button"/>
- </StackLayout>
Step 4
Next, open Solution Explorer >> Project Name >> MainPage.xaml.cs. Then, click to replace the C# code.The code is given below.
The code is given below.
C# Code
- using Plugin.TextToSpeech;
- using System;
- using Xamarin.Forms;
-
- namespace TextToSpeech
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
- private void ClickedButton (object Sender, EventArgs args)
- {
- var text = Enter.Text;
-
- CrossTextToSpeech.Current.Speak(text);
- }
- }
- }
Step 5
Press F5 or Build and Run the application.
Output
Run the given application and type some content in the textbox. Click "Speak" button. The app starts reading the given text.
Finally, we have successfully created a Xamarin.Forms Text To Speech app.