Use byte arrays instead of base64 for .NET / JS exchange

This commit is contained in:
Thorsten Sommer 2026-01-06 15:53:11 +01:00
parent 84e2a96f49
commit fcbcaeb20a
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 5 additions and 9 deletions

View File

@ -428,7 +428,7 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan
}
[JSInvokable]
public async Task OnAudioChunkReceived(string base64Chunk)
public async Task OnAudioChunkReceived(byte[] chunkBytes)
{
if (this.currentRecordingStream is null)
{
@ -438,10 +438,9 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan
try
{
var chunkBytes = Convert.FromBase64String(base64Chunk);
await this.currentRecordingStream.WriteAsync(chunkBytes);
await this.currentRecordingStream.FlushAsync();
this.Logger.LogDebug("Wrote {ByteCount} bytes to recording stream.", chunkBytes.Length);
}
catch (Exception ex)

View File

@ -93,13 +93,10 @@ window.audioRecorder = {
mediaRecorder.ondataavailable = async (event) => {
if (event.data.size > 0) {
const arrayBuffer = await event.data.arrayBuffer();
const base64 = btoa(
new Uint8Array(arrayBuffer).reduce((data, byte) => data + String.fromCharCode(byte), '')
);
// Send chunk to .NET immediately:
const uint8Array = new Uint8Array(arrayBuffer);
try {
await dotnetReference.invokeMethodAsync('OnAudioChunkReceived', base64);
await dotnetReference.invokeMethodAsync('OnAudioChunkReceived', uint8Array);
} catch (error) {
console.error('Error sending audio chunk to .NET:', error);
}