|
1 | 1 | @page "/"
|
| 2 | + |
2 | 3 | @using Microsoft.Extensions.AI
|
3 |
| -@inject IJSRuntime JSRuntime |
4 | 4 |
|
| 5 | +@inject IJSRuntime JSRuntime |
5 | 6 | @inject ISpeechToTextClient SpeechToTextClient
|
6 | 7 |
|
7 | 8 | <TelerikTextArea @bind-Value="@TextValue"
|
|
17 | 18 | </TextAreaSuffixTemplate>
|
18 | 19 | </TelerikTextArea>
|
19 | 20 |
|
20 |
| - |
21 |
| - |
22 | 21 | @code {
|
23 | 22 | private string TextValue { get; set; } = string.Empty;
|
24 | 23 | private DotNetObjectReference<Home>? dotNetObjectReference;
|
25 | 24 |
|
26 |
| - private async void OnStartHandler() |
| 25 | + private async Task OnStartHandler() |
27 | 26 | {
|
28 | 27 | await JSRuntime.InvokeVoidAsync("speechRecognitionStarted");
|
29 | 28 | }
|
30 | 29 |
|
31 |
| - private async void OnEndHandler() |
| 30 | + private async Task OnEndHandler() |
32 | 31 | {
|
33 | 32 | await JSRuntime.InvokeVoidAsync("speechRecognitionEnded");
|
34 | 33 | }
|
|
37 | 36 | {
|
38 | 37 | if (firstRender)
|
39 | 38 | {
|
40 |
| - await JSRuntime.InvokeVoidAsync("initializeSpeechToTextButton"); |
41 |
| - |
42 |
| - dotNetObjectReference = DotNetObjectReference.Create(this); |
43 |
| - |
44 |
| - await JSRuntime.InvokeVoidAsync("setDotNetObjectReference", dotNetObjectReference); |
| 39 | + try |
| 40 | + { |
| 41 | + await JSRuntime.InvokeVoidAsync("initializeSpeechToTextButton"); |
| 42 | + |
| 43 | + dotNetObjectReference = DotNetObjectReference.Create(this); |
| 44 | + |
| 45 | + await JSRuntime.InvokeVoidAsync("setDotNetObjectReference", dotNetObjectReference); |
| 46 | + } |
| 47 | + catch (Exception ex) |
| 48 | + { |
| 49 | + Console.Error.WriteLine($"JSInterop failed: {ex.Message}"); |
| 50 | + } |
45 | 51 | }
|
46 | 52 |
|
47 | 53 | await base.OnAfterRenderAsync(firstRender);
|
|
55 | 61 | return;
|
56 | 62 | }
|
57 | 63 |
|
58 |
| - using var stream = new MemoryStream(audioBytes); |
59 |
| - |
60 | 64 | try
|
61 | 65 | {
|
| 66 | + using var stream = new MemoryStream(audioBytes); |
| 67 | + |
62 | 68 | await GetSpeechToTextResponse(stream);
|
63 | 69 | }
|
64 | 70 | catch (Exception e)
|
|
96 | 102 | this.mediaRecorder.ondataavailable = (e) => this.audioChunks.push(e.data);
|
97 | 103 | this.mediaRecorder.onstop = async () => {
|
98 | 104 | if (this.mediaRecorder) {
|
99 |
| - if (!this.recordingAborted) { |
100 |
| - const audioBlob = new Blob(this.audioChunks, { type: 'audio/wav' }); |
101 |
| - const arrayBuffer = await audioBlob.arrayBuffer(); |
102 |
| - const uint8Array = new Uint8Array(arrayBuffer); |
103 |
| - // Call back to Blazor with the recorded audio data |
104 |
| - try { |
105 |
| - window.dotNetObjectReference.invokeMethodAsync("OnRecordedAudio", uint8Array); |
106 |
| - console.log("Successfully called OnRecordedAudio via component reference"); |
107 |
| - } catch (error) { |
108 |
| - console.error("Error calling OnRecordedAudio:", error); |
| 105 | + const audioBlob = new Blob(this.audioChunks, { type: 'audio/wav' }); |
| 106 | + const arrayBuffer = await audioBlob.arrayBuffer(); |
| 107 | + const uint8Array = new Uint8Array(arrayBuffer); |
| 108 | + // Call back to Blazor with the recorded audio data |
| 109 | + try { |
| 110 | + if (window.dotNetObjectReference) { |
| 111 | + await window.dotNetObjectReference.invokeMethodAsync("OnRecordedAudio", uint8Array); |
| 112 | + } else { |
| 113 | + console.warn("dotNetObjectReference is not set."); |
109 | 114 | }
|
| 115 | + } catch (error) { |
| 116 | + console.error("Error calling OnRecordedAudio:", error); |
110 | 117 | }
|
111 | 118 | this.audioChunks = [];
|
112 | 119 | this.unbindMediaRecorderEvents();
|
|
152 | 159 | // Event callbacks
|
153 | 160 | onStart() {
|
154 | 161 | // add any additional logic here if necessary
|
155 |
| - console.log("Media recorder started"); |
| 162 | + console.log("Media recorder started"); |
156 | 163 | },
|
157 | 164 |
|
158 | 165 | onEnd() {
|
159 | 166 | // add any additional logic here if necessary
|
160 |
| - console.log("Media recorder ended"); |
| 167 | + console.log("Media recorder ended"); |
161 | 168 | },
|
162 | 169 |
|
163 | 170 | // Public API methods
|
|
0 commit comments