Blazor Bootstrap 組件庫語音組件介紹

Speech 語音識別與合成

通過麥克風語音採集轉換為文字(STT),或者通過文字通過語音朗讀出來(TTS)
本組件依賴於 BootstrapBlazor.AzureSpeech,使用本組件時需要引用其組件包

Demo //www.blazor.zone/speechs

Nuget 包安裝

使用 nuget.org 進行 BootstrapBlazor.AzureSpeech 組件的安裝

.NET CLI

dotnet add package BootstrapBlazor.AzureSpeech

PackageReference

<PackageReference Include="BootstrapBlazor.AzureSpeech" />

註冊服務

Program.cs

// 增加語音服務
builder.Services.AddBootstrapBlazorAzureSpeech();

使用服務

// 語音識別服務
[Inject]
[NotNull]
private RecognizerService? RecognizerService { get; set; }

// 語音合成服務
[Inject]
[NotNull]
private SynthesizerService? SynthesizerService { get; set; }

1. Recognizer 語音識別, 通過語音採集設備將語音轉化為文本

點擊開始識別後對錄入語音進行識別

@page "/recognizers"
@using BootstrapBlazor.Components
@using Microsoft.AspNetCore.Components

<h3>Recognizer 語音識別</h3>

<h4>通過語音採集設備將語音轉化為文本</h4>

    <p><b>使用說明:</b></p>
    <p>點擊 <b>開始識別</b> 後,腳本運行可能會提示要求錄音許可權,授權後開始講話,稍等一下,右側文本框顯示語音識別結果,請勿講完話後直接點擊 <b>結束識別</b></p>
    <div class="row">
        <div class="col-12 col-sm-6 text-center">
            <SpeechWave Show="Start" OnTimeout="OnTimeout" class="my-3" />
            <Button Text="@ButtonText" OnClick="OnStart" />
        </div>
        <div class="col-12 col-sm-6">
            <Textarea @bind-Value="Result" rows="6" />
        </div>
    </div> 

@code{ 
    [Inject]
    [NotNull]
    private RecognizerService? RecognizerService { get; set; }

    private bool Start { get; set; }

    private string? Result { get; set; }

    private string ButtonText { get; set; } = "開始識別";

    private async Task OnStart()
    {
        if (ButtonText == "開始識別")
        {
            Start = true;
            ButtonText = "結束識別";
            await RecognizerService.RecognizeOnceAsync(Recognize);
        }
        else
        {
            await Close();
        }
    }

    private async Task OnTimeout()
    {
        await Close();
    }

    private Task Recognize(string result)
    {
        Result = result;
        Start = false;
        ButtonText = "開始識別";
        StateHasChanged();
        return Task.CompletedTask;
    }

    private async Task Close()
    {
        await RecognizerService.CloseAsync(Recognize);
    }
}

2.Synthesizer 語音合成,將文本內容轉化為語音

點擊開始合成後對文本內容進行語音合成

@page "/synthesizers"
@using BootstrapBlazor.Components
@using Microsoft.AspNetCore.Components

<h3>Synthesizer 語音合成</h3>

<h4>將文本內容轉化為語音</h4>

    <div class="row">
        <div class="col-12 col-sm-6">
            <Textarea @bind-Value="InputText" rows="6" />
        </div>
        <div class="col-12 col-sm-6 text-center">
            <SpeechWave Show="Start" ShowUsedTime="false" class="my-3" />
            <Button Text="@ButtonText" OnClick="OnStart" IsDisabled="@IsDisabled" Icon="@ButtonIcon" />
        </div>
    </div>

@code{
    [Inject]
    [NotNull]
    private SynthesizerService? SynthesizerService { get; set; }

    private bool Start { get; set; }

    private string? InputText { get; set; }

    private string ButtonText { get; set; } = "開始合成";

    private string ButtonIcon { get; set; } = "fa fa-fw fa-microphone";

    private bool IsDisabled { get; set; }

    private async Task OnStart()
    {
        if (ButtonText == "開始合成")
        {
            IsDisabled = true;
            ButtonIcon = "fa fa-fw fa-spin fa-spinner";
            await SynthesizerService.SynthesizerOnceAsync(InputText, Recognize);
        }
        else
        {
            await Close();
        }
    }

    private Task Recognize(SynthesizerStatus status)
    {
        if (status == SynthesizerStatus.Synthesizer)
        {
            Start = true;
            IsDisabled = false;
            ButtonIcon = "fa fa-fw fa-spin fa-spinner";
            ButtonText = "停止合成";
        }
        else
        {
            Start = false;
            IsDisabled = false;
            ButtonIcon = "fa fa-fw fa-microphone";
            ButtonText = "開始合成";
        }
        StateHasChanged();
        return Task.CompletedTask;
    }

    private async Task Close()
    {
        await SynthesizerService.CloseAsync(Recognize);
    }
}

Blazor Bootstrap 組件庫文檔

//www.blazor.zone

寫在最後

  希望大佬們看到這篇文章,能給項目點個star支援下,感謝各位!

star流程:

1、訪問點擊項目鏈接:BootstrapBlazor

  //gitee.com/LongbowEnterprise/BootstrapBlazor

2、點擊star,如下圖,即可完成star,關注項目不迷路:
  
  

另外還有兩個GVP項目,大佬們方便的話也點下star唄,非常感謝:

  BootstrapAdmin 項目地址:

  //gitee.com/LongbowEnterprise/BootstrapAdmin

  SliderCaptcha 項目地址:

  //gitee.com/LongbowEnterprise/SliderCaptcha

交流群(QQ)歡迎加群討論

   BA & Blazor ①(795206915)        BA & Blazor ②(675147445)