mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-14 23:01:36 +00:00
Fixed issue while storing the last chunk
This commit is contained in:
parent
b59d120b9a
commit
75b5e40379
@ -30,6 +30,7 @@ window.scrollToBottom = function(element) {
|
|||||||
let mediaRecorder;
|
let mediaRecorder;
|
||||||
let actualRecordingMimeType;
|
let actualRecordingMimeType;
|
||||||
let changedMimeType = false;
|
let changedMimeType = false;
|
||||||
|
let pendingChunkUploads = 0;
|
||||||
|
|
||||||
window.audioRecorder = {
|
window.audioRecorder = {
|
||||||
start: async function (dotnetRef, desiredMimeTypes = []) {
|
start: async function (dotnetRef, desiredMimeTypes = []) {
|
||||||
@ -84,17 +85,22 @@ window.audioRecorder = {
|
|||||||
} else {
|
} else {
|
||||||
changedMimeType = false;
|
changedMimeType = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset the pending uploads counter:
|
||||||
|
pendingChunkUploads = 0;
|
||||||
|
|
||||||
// Stream each chunk directly to .NET as it becomes available:
|
// Stream each chunk directly to .NET as it becomes available:
|
||||||
mediaRecorder.ondataavailable = async (event) => {
|
mediaRecorder.ondataavailable = async (event) => {
|
||||||
if (event.data.size > 0) {
|
if (event.data.size > 0) {
|
||||||
const arrayBuffer = await event.data.arrayBuffer();
|
pendingChunkUploads++;
|
||||||
const uint8Array = new Uint8Array(arrayBuffer);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const arrayBuffer = await event.data.arrayBuffer();
|
||||||
|
const uint8Array = new Uint8Array(arrayBuffer);
|
||||||
await dotnetRef.invokeMethodAsync('OnAudioChunkReceived', uint8Array);
|
await dotnetRef.invokeMethodAsync('OnAudioChunkReceived', uint8Array);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error sending audio chunk to .NET:', error);
|
console.error('Error sending audio chunk to .NET:', error);
|
||||||
|
} finally {
|
||||||
|
pendingChunkUploads--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -109,6 +115,14 @@ window.audioRecorder = {
|
|||||||
// Add an event listener to handle the stop event:
|
// Add an event listener to handle the stop event:
|
||||||
mediaRecorder.onstop = async () => {
|
mediaRecorder.onstop = async () => {
|
||||||
|
|
||||||
|
// Wait for all pending chunk uploads to complete before finalizing:
|
||||||
|
console.log(`Audio recording - waiting for ${pendingChunkUploads} pending uploads.`);
|
||||||
|
while (pendingChunkUploads > 0) {
|
||||||
|
await new Promise(r => setTimeout(r, 10)); // wait 10 ms before checking again
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Audio recording - all chunks uploaded, finalizing.');
|
||||||
|
|
||||||
// Stop all tracks to release the microphone:
|
// Stop all tracks to release the microphone:
|
||||||
mediaRecorder.stream.getTracks().forEach(track => track.stop());
|
mediaRecorder.stream.getTracks().forEach(track => track.stop());
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user