diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj index 7edb69a0091e2ac36871a3288a12e44786dd246c..d7523b5da2ee675d4694e2d9c67fee5c2373f83c 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj +++ b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj @@ -1,7 +1,7 @@ - 6.0.5 + 6.0.6 diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs index 73556028a697dfe9148fed297ad0b2a5e076b522..4345e142b1fcac52f3923f341d4bb83010223acd 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs +++ b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.JSInterop; using System.Text; @@ -26,16 +27,20 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable private Baidu.Aip.Speech.Asr Client { get; } + private ILogger Logger { get; } + /// /// 构造函数 /// /// /// - public BaiduRecognizerProvider(IOptionsMonitor options, IJSRuntime runtime) + /// + public BaiduRecognizerProvider(IOptionsMonitor options, IJSRuntime runtime, ILogger logger) { JSRuntime = runtime; SpeechOption = options.CurrentValue; Client = new Baidu.Aip.Speech.Asr(SpeechOption.AppId, SpeechOption.ApiKey, SpeechOption.Secret); + Logger = logger; } /// @@ -46,18 +51,19 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable /// public async Task InvokeAsync(RecognizerOption option) { - if (string.IsNullOrEmpty(option.MethodName)) - { - throw new InvalidOperationException(); - } - - Option = option; - if (Module == null) + if (!string.IsNullOrEmpty(option.MethodName)) { - Module = await JSRuntime.InvokeAsync("import", "/_content/BootstrapBlazor.BaiduSpeech/js/recognizer.js"); + Option = option; + if (Module == null) + { + var moduleName = "./_content/BootstrapBlazor.BaiduSpeech/js/recognizer.js"; + Logger.LogInformation($"load module {moduleName}"); + Module = await JSRuntime.InvokeAsync("import", moduleName); + } + Interop ??= DotNetObjectReference.Create(this); + await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(RecognizeCallback), Option.AutoRecoginzerElapsedMilliseconds); + Logger.LogInformation($"{Option.MethodName}"); } - Interop ??= DotNetObjectReference.Create(this); - await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(RecognizeCallback), Option.AutoRecoginzerElapsedMilliseconds); } /// @@ -66,17 +72,27 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable [JSInvokable] public async Task RecognizeCallback(RecognizerStatus status, byte[]? bytes) { + Logger.LogInformation($"RecognizerStatus: {status}"); string data = "Error"; if (status == RecognizerStatus.Finished) { var result = Client.Recognize(bytes, "wav", 16000); - var sb = new StringBuilder(); - var text = result["result"].ToArray(); - foreach (var item in text) + var err_no = result.Value("err_no"); + if (err_no == 0) + { + var sb = new StringBuilder(); + var text = result["result"].ToArray(); + foreach (var item in text) + { + sb.Append(item.ToString()); + } + data = sb.ToString(); + Logger.LogInformation($"recognizer: {data}"); + } + else { - sb.Append(item.ToString()); + Logger.LogError($"err_no: {err_no}"); } - data = sb.ToString(); } if (Option.Callback != null) diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs index 1aa03b8b2ddd080d946f15d81c35f78ae4ad1310..8f8c35587a096fc29c0f75a7c2f8575e35ee17dd 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs +++ b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.JSInterop; @@ -25,16 +26,20 @@ public class BaiduSynthesizerProvider : ISynthesizerProvider, IAsyncDisposable private Baidu.Aip.Speech.Tts Client { get; } + private ILogger Logger { get; } + /// /// 构造函数 /// /// /// - public BaiduSynthesizerProvider(IOptionsMonitor options, IJSRuntime runtime) + /// + public BaiduSynthesizerProvider(IOptionsMonitor options, IJSRuntime runtime, ILogger logger) { JSRuntime = runtime; SpeechOption = options.CurrentValue; Client = new Baidu.Aip.Speech.Tts(SpeechOption.ApiKey, SpeechOption.Secret); + Logger = logger; } /// @@ -49,19 +54,34 @@ public class BaiduSynthesizerProvider : ISynthesizerProvider, IAsyncDisposable // 加载模块 if (Module == null) { - Module = await JSRuntime.InvokeAsync("import", "./_content/BootstrapBlazor.BaiduSpeech/js/synthesizer.js"); + var moduleName = "./_content/BootstrapBlazor.BaiduSpeech/js/synthesizer.js"; + Logger.LogInformation($"load module {moduleName}"); + Module = await JSRuntime.InvokeAsync("import", moduleName); } Interop ??= DotNetObjectReference.Create(this); if (Option.MethodName == "bb_baidu_speech_synthesizerOnce" && !string.IsNullOrEmpty(Option.Text)) { var result = Client.Synthesis(Option.Text); - await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(Callback), result.Data); + if (result.Success) + { + await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(Callback), result.Data); + } + else + { + + } + Logger.LogInformation($"bb_baidu_speech_synthesizerOnce {result.Success}"); + if (!result.Success) + { + Logger.LogError($"{result.ErrorCode}: {result.ErrorMsg}"); + } } else if (Option.MethodName == "bb_baidu_close_synthesizer") { // 停止语音 await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(Callback)); + Logger.LogInformation("bb_baidu_close_synthesizer"); } }