Toolbelt.Blazor.SpeechSynthesis 11.0.0

dotnet add package Toolbelt.Blazor.SpeechSynthesis --version 11.0.0
                    
NuGet\Install-Package Toolbelt.Blazor.SpeechSynthesis -Version 11.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Toolbelt.Blazor.SpeechSynthesis" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Toolbelt.Blazor.SpeechSynthesis" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Toolbelt.Blazor.SpeechSynthesis" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Toolbelt.Blazor.SpeechSynthesis --version 11.0.0
                    
#r "nuget: Toolbelt.Blazor.SpeechSynthesis, 11.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Toolbelt.Blazor.SpeechSynthesis@11.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Toolbelt.Blazor.SpeechSynthesis&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Toolbelt.Blazor.SpeechSynthesis&version=11.0.0
                    
Install as a Cake Tool

Blazor SpeechSynthesis NuGet Package

Summary

This is a class library for Blazor app (both "WebAssembly App" client-side model and "Server App" server-side model) to provide Speech Synthesis API access.

How to install and use?

1. Installation and Registration

Step.1-1 Install the library via NuGet package, like this.

> dotnet add package Toolbelt.Blazor.SpeechSynthesis

Step.1-2 Register "SpeechSynthesis" service into the DI container.

// Program.cs
...
using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...
...
var builder = WebApplication.CreateDefault(args);
...
builder.Services.AddSpeechSynthesis(); // <- Add this line.
...

2. Usage in your Blazor component (.razor)

Step.2-1 Open the Toolbelt.Blazor.SpeechSynthesis namespace, and inject the SpeechSynthesis service into the component.

@{/* This is your component .razor */}
@using Toolbelt.Blazor.SpeechSynthesis @{/* Add these two lines. */}
@inject SpeechSynthesis SpeechSynthesis
...

Step.2-2 Invoke Speak() method of the SpeechSynthesis service instance to speak!

@using Toolbelt.Blazor.SpeechSynthesis
@inject SpeechSynthesis SpeechSynthesis

<div> <textarea @bind="Text"></textarea> </div>

<div> <button @onclick="onClickSpeak">Speak</button> </div>

@code {

  string Text;

  async Task onClickSpeak() {
    await this.SpeechSynthesis.SpeakAsync(this.Text); // 👈 Speak!
  }
}

You can also speak with detail parameters, such as pitch, rate, volume, by using SpeechSynthesisUtterance object.

  async Task onClickSpeak() {
    var utterancet = new SpeechSynthesisUtterance {
        Text = this.Text,
        Lang = "en-US", // BCP 47 language tag
        Pitch = 1.0, // 0.0 ~ 2.0 (Default 1.0)
        Rate = 1.0, // 0.1 ~ 10.0 (Default 1.0)
        Volume = 1.0 // 0.0 ~ 1.0 (Default 1.0)
    }
    await this.SpeechSynthesis.SpeakAsync(utterancet); // 👈 Speak!
  }

If you want to chose type of voices, you can do it with GetVoicesAsync() method of SpeechSynthesis service instance.

  IEnumerable<SpeechSynthesisVoice> Voices;

  protected async override Task OnAfterRenderAsync(bool firstRender)
  {
    if (firstRender)
    {
      this.Voices = await this.SpeechSynthesis.GetVoicesAsync();
      this.StateHasChanged();
    }
  }

  async Task onClickSpeak() {
    var utterancet = new SpeechSynthesisUtterance {
        Text = this.Text,
        Voice = this.Voices.FirstOrDefault(v => v.Name.Contains("Haruka"));
    }
    await this.SpeechSynthesis.SpeakAsync(utterancet); // 👈 Speak with "Haruka"'s voice!
  }

JavaScript file cache busting

This library includes and uses a JavaScript file to access the browser's Speech Synthesis API. When you update this library to a newer version, the browser may use the cached previous version of the JavaScript file, leading to unexpected behavior. To prevent this issue, the library appends a version query string to the JavaScript file URL when loading it.

.NET 8 and 9

A version query string will always be appended to this library's JavaScript file URL regardless of the Blazor hosting model you are using.

.NET 10 or later

By default, a version query string will be appended to the JavaScript file URL that this library loads. If you want to disable appending a version query string to the JavaScript file URL, you can do so by setting the TOOLBELT_BLAZOR_SPEECHSYNTHESIS_JSCACHEBUSTING environment variable to 0.

// Program.cs
...
// 👇 Add this line to disable appending a version query string for this library's JavaScript file.
Environment.SetEnvironmentVariable("TOOLBELT_BLAZOR_SPEECHSYNTHESIS_JSCACHEBUSTING", "0");

var builder = WebApplication.CreateBuilder(args);
...

However, when you publish a .NET 10 Blazor WebAssembly app, a version query string will always be appended to the JavaScript file URL regardless of the TOOLBELT_BLAZOR_SPEECHSYNTHESIS_JSCACHEBUSTING environment variable setting. The reason is that published Blazor WebAssembly standalone apps don't include import map entries for JavaScript files from NuGet packages. If you want to avoid appending a version query string to the JavaScript file URL in published Blazor WebAssembly apps, you need to set the ToolbeltBlazorSpeechSynthesisJavaScriptCacheBusting MSBuild property to false in the project file of the Blazor WebAssembly app, like this:

<PropertyGroup>
  <ToolbeltBlazorSpeechSynthesisJavaScriptCacheBusting>false</ToolbeltBlazorSpeechSynthesisJavaScriptCacheBusting>
</PropertyGroup>

Why do we append a version query string to the JavaScript file URL regardless of whether the import map is available or not?

We know that .NET 9 or later allows us to use import maps to import JavaScript files with a fingerprint in their file names. Therefore, in .NET 9 or later Blazor apps, you may want to avoid appending a version query string to the JavaScript file URL that this library loads.

However, we recommend keeping the default behavior of appending a version query string to the JavaScript file URL. The reason is that published Blazor WebAssembly standalone apps don't include import map entries for JavaScript files from NuGet packages. This inconsistent behavior between development and production environments and hosting models may lead to unexpected issues that are hard to diagnose, particularly in AutoRender mode apps.

Release Note

Release notes is here.

License

Mozilla Public License Version 2.0

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Toolbelt.Blazor.SpeechSynthesis:

Repository Stars
ArduPilot/MissionPlanner
Mission Planner Ground Control Station for ArduPilot (c# .net)
Version Downloads Last Updated
11.0.0 1,876 1/2/2026
10.3.1 18,718 11/26/2023
10.3.0 348 11/16/2023
10.2.0 1,222 9/10/2023
10.1.0 319 9/5/2023
10.0.0 8,555 7/2/2022
9.0.4 1,501 5/29/2022
9.0.3 743 5/18/2022
9.0.2 744 5/10/2022
9.0.1 4,521 1/3/2022
9.0.0 1,887 9/5/2021
8.1.1 1,472 5/8/2021
8.1.0 6,675 12/6/2020
8.0.0 14,870 1/8/2020
7.0.0.1 1,815 9/6/2019
7.0.0 528 9/6/2019
6.0.0 665 8/19/2019
5.0.0 573 6/16/2019
4.0.0 814 5/2/2019
3.0.0 818 3/9/2019
Loading failed

v.11.0.0
- Update: Added support for .NET 10, and dropped support for .NET 7 or earlier.
- Improve: Removed the dependency on the Toolbelt.Blazor.GetProperty.Script NuGet package for .NET 10 or later projects.
- Improve: Better support for controlling JavaScript caching strategies on .NET 10 or later projects.

To see all the change logs, please visit the following URL.
- https://github.com/jsakamoto/Toolbelt.Blazor.SpeechSynthesis/blob/master/RELEASE-NOTES.txt