Using the System.Speech NuGet package, you may add speech capability to your.NET MAUI application for WinUI devices.
I’ll demonstrate in this article how to include System.Speech into a.NET MAUI application that uses.NET 8.
It should be noted that only WinUI (Windows) devices are compatible with the System.Speech NuGet package.
For instance, the application described in this guide will send the user a kind welcome.
This code can be found below:
const int PDefaultRate = 0;
private static bool initHello {get;set;} = false;
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
if (!initHello)
{
InitTimer();
}
}
private async void InitTimer()
{
while (Microsoft.Maui.Controls.Application.Current == null)
{
await Task.Delay(500);
}
Appearing();
}
private void Appearing()
{
IDispatcherTimer? timer = Microsoft.Maui.Controls.Application.Current?.Dispatcher.CreateTimer();
if (timer != null)
{
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (sender, _) =>
{
if (sender is IDispatcherTimer tmr)
{
tmr.Stop();
WelCome();
}
};
timer.Start();
}
}
private void WelCome()
{
if (initHello) return;
initHello = true;
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split("\\")[1];
string appName = AppInfo.Name;
var voice = new System.Speech.Synthesis.SpeechSynthesizer();
var voices = voice.GetInstalledVoices();
if (voices != null && voices.Count > 0) voice.SelectVoice(voices[1].VoiceInfo.Name);
voice.SetOutputToDefaultAudioDevice();
voice.Rate = PDefaultRate;
voice.Speak($"Hello {userName}, welcome to my .NET MAUI {appName} application!");
}
In the code
- Line 3: We establish an initializer to avoid repeating the speech.
- Line 15: A Timer is initialized to delay execution until the Application is fully started, as indicated by the Application.Current not returning null.
- Line 33: Another Timer is set up for loading the message to the interface asynchronously.
- Lines 58-63: This segment triggers the speech function. Specifically, in line 60, we select the second voice option, which is typically a female voice.
The MainPage also has to have the following code added to it.
public MainPage()
{
InitializeComponent();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
DevicePlatform? platform = DeviceInfo.Platform;
if (platform != null && platform == DevicePlatform.WinUI)
{
GoodBye();
}
}
public void GoodBye()
{
var voice = new System.Speech.Synthesis.SpeechSynthesizer();
var voices = voice.GetInstalledVoices();
if (voices != null && voices.Count > 0) voice.SelectVoice(voices[1].VoiceInfo.Name);
voice.SetOutputToDefaultAudioDevice();
voice.Rate = 0;
voice.Speak($"See you later!");
}
In the code, line 10 checks if the Application runs on a Windows platform, and line 12 triggers the GoodBye() function.
In summary
For Windows.NET MAUI apps, adding voice functionality is a feasible and reasonably easy task. For Windows-based apps, this functionality opens up a world of interactive and accessible possibilities.
It is also possible to expand this feature to other platforms.
Should the community exhibit a strong desire, Microsoft might consider such a system.Other operating systems can access speech through.NET MAUI. This development would expand the compatibility and platform reach of.NET MAUI applications.
This article has the source code attached. Please test it and utilize it as necessary. Thank you for your inquiry, and we hope this is helpful. Recall that Progress Telerik and Microsoft worked together to create the.NET MAUI framework.