1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- const { getAssemblyExports } = await globalThis.getDotnetRuntime(0);
- var nameSpace = (await getAssemblyExports("SimpleWebChat.BlazorWasm.dll")).SimpleWebChat.BlazorWasm.Modules;
- var DotNetInterop = nameSpace.AudioCaptureModule;
- var isRunning = false;
- var stream;
- var recorder;
- export async function init() {
- if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia)
- stream = await navigator.mediaDevices.getUserMedia({ audio: true });
- else throw "failure to getUserMedia";
- if (stream) {
- recorder = new MediaRecorder(stream, {
- audioBitsPerSecond: 64000,
- mimeType: 'audio/webm;codecs=OPUS'
- });
- recorder.ondataavailable = async (r) => {
- DotNetInterop.JsFeedChunk(new Uint8Array(await r.data.arrayBuffer()));
- };
- recorder.onstop = () => {
- if (isRunning) {
- recorder.start();
- setTimeout(() => {
- if (isRunning) recorder.stop();
- }, 1000);
- }
- };
- }
- return !!stream;
- }
- export async function start() {
- isRunning = true;
- if (recorder.state != 'recording') {
- recorder.start();
- setTimeout(() => {
- if (isRunning) recorder.stop();
- }, 1000);
- }
- }
- export async function stop() {
- isRunning = false;
- recorder.stop();
- }
|