Skip to content

Commit

Permalink
feat(WebSpeechRecognition): add Callback parameter (#4162)
Browse files Browse the repository at this point in the history
* refactor: 更正语音包设置

* refactor: 增加回调事件条件

* chore: bump version 8.0.0

* chore: 更新 WinBox 依赖包

* chore: bump version 8.8.5-beta01
  • Loading branch information
ArgoZhang authored Aug 27, 2024
1 parent 5f41cfb commit d58768c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ protected override async Task OnInitializedAsync()
_speechVoices.AddRange(voices);
}

_voices.AddRange(_speechVoices.Select(i => new SelectedItem(i.Name!, $"{i.Name}({i.Lang})")));
_voiceName = _speechVoices.Find(i => i.Lang == CultureInfo.CurrentUICulture.Name)?.Name;
_voices.AddRange(_speechVoices.Select(i => new SelectedItem($"{i.Name}({i.Lang})", $"{i.Name}({i.Lang})")));
var voice = _speechVoices.Find(i => i.Lang == CultureInfo.CurrentUICulture.Name);
if (voice != null)
{
_voiceName = $"{voice.Name}({voice.Lang})";
}

_text = Localizer["WebSpeechText"];
_buttonText = Localizer["WebSpeechSpeakButtonText"];
Expand Down Expand Up @@ -137,7 +141,7 @@ private async Task OnStart()
_star = true;
StateHasChanged();

await _entry.SpeakAsync(_text, _speechVoices.Find(i => i.Name == _voiceName));
await _entry.SpeakAsync(_text, _speechVoices.Find(i => $"{i.Name}({i.Lang})" == _voiceName));
await _tcs.Task;
_star = false;
_tcs = null;
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.8.4</Version>
<Version>8.8.5-beta01</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public async Task StartAsync(WebSpeechRecognitionOption option)
await module.InvokeVoidAsync("start", _id, _interop, new
{
TriggerStart = OnStartAsync != null,
TriggerSpeechStart = OnSpeechStartAsync != null
TriggerSpeechStart = OnSpeechStartAsync != null,
TriggerSpeechEnd = OnSpeechEndAsync != null,
TriggerNoMatch = OnNoMatchAsync != null,
TriggerEnd = OnEndAsync != null,
TriggerError = OnErrorAsync != null
}, option);
}

Expand Down
34 changes: 22 additions & 12 deletions src/BootstrapBlazor/wwwroot/modules/recognition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@ export async function start(id, invoke, trigger, option) {
}
recognition.onspeechend = () => {
recognition.stop();
invoke.invokeMethodAsync("TriggerSpeechEndCallback");
if (trigger.triggerSpeechEnd) {
invoke.invokeMethodAsync("TriggerSpeechEndCallback");
}
}
recognition.onnomatch = e => {
invoke.invokeMethodAsync("TriggerNoMatchCallback", {
error: 'no-match',
message: 'No match found.'
});
Data.remove(id);
if (trigger.triggerNoMatch) {
invoke.invokeMethodAsync("TriggerNoMatchCallback", {
error: 'no-match',
message: 'No match found.'
});
}
}
recognition.onend = () => {
invoke.invokeMethodAsync("TriggerEndCallback");
Data.remove(id);
if (trigger.triggerEnd) {
invoke.invokeMethodAsync("TriggerEndCallback");
}
}
recognition.onerror = e => {
invoke.invokeMethodAsync("TriggerErrorCallback", {
error: e.error,
message: e.message
});
Data.remove(id);
if (trigger.triggerError) {
invoke.invokeMethodAsync("TriggerErrorCallback", {
error: e.error,
message: e.message
});
}
}
recognition.onresult = e => {
let final_transcript = '';
Expand Down Expand Up @@ -69,9 +80,8 @@ export async function start(id, invoke, trigger, option) {
if (continuous !== void 0) {
recognition.continuous = continuous;
}
recognition.start();

Data.set(id, recognition);
recognition.start();
}

export function stop(id) {
Expand Down

0 comments on commit d58768c

Please sign in to comment.