Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to Restrict Azure Speech SDK AudioConfig to Only System Audio and Exclude Microphone Input?

$
0
0

Question:I am working on a Blazor project where I integrate Azure Speech Service to perform speech-to-text transcription on system audio during screen sharing. However, I am facing an issue where audio from the microphone is still being captured, even though I've explicitly disabled it in the JavaScript code that manages the screen sharing.

Here's the flow of my implementation:

JavaScript Code (Screen Sharing)This function enables screen sharing with system audio, while explicitly disabling the microphone tracks:

window.startScreenSharing = async function () {    try {        const container = document.getElementById('sharedScreen');        if (!container) {            console.error('Element with ID "sharedScreen" not found');            return false;        }        // Start screen sharing with video and system audio        const stream = await navigator.mediaDevices.getDisplayMedia({            video: true,            audio: true  // Request system audio along with video        });        // Disable microphone tracks        stream.getAudioTracks().forEach(track => track.enabled = false);        // Display the shared screen in a video element        const videoElement = document.createElement('video');        videoElement.srcObject = stream;        videoElement.autoplay = true;        videoElement.muted = true;  // Mute local audio to avoid feedback        videoElement.controls = false;        videoElement.style.width = '100%';        videoElement.style.height = '100%';        container.innerHTML = '';        container.appendChild(videoElement);        window.currentScreenStream = stream;        return true;    } catch (err) {        console.error('Screen capture error:', err);        alert('Screen capture failed. Check your browser settings.');        return false;    }};

Blazor Page (C# Method to Start Screen Sharing)The StartScreenSharing method in Blazor calls the above JavaScript function and initiates transcription:

  private async Task StartScreenSharing()    {        try        {            Console.WriteLine("Starting screen sharing...");            bool started = await JSRuntime.InvokeAsync<bool>("startScreenSharing");            if (started)            {                isScreenSharing = true;                StateHasChanged();                StartTimer();                _speechService = new SpeechToTextService();                 await _speechService.StartRecognitionAsync(_cancellationTokenSource.Token);            }            else            {                Console.WriteLine("Failed to start screen sharing.");            }        }        catch (Exception ex)        {            Console.WriteLine($"Error starting screen sharing: {ex.Message}");        }    }

Azure Speech SDK (C# Code)This is the code where I configure the Azure Speech SDK for transcription:

 public async Task StartRecognitionAsync(CancellationToken cancellationToken)    {        var config = SpeechConfig.FromSubscription(subscriptionKey, region);        // Using default speaker output        using var audioConfig = AudioConfig.FromDefaultSpeakerOutput();        recognizer = new SpeechRecognizer(config, audioConfig);        recognizer.Recognizing += async (s, e) =>        {            if (!string.IsNullOrEmpty(e.Result.Text))            {                Console.WriteLine($"Recognized: {e.Result.Text}");            }        };        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);        cancellationToken.Register(async () =>        {            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);        });    }

The ProblemDespite disabling the microphone in JavaScript (stream.getAudioTracks().forEach(track => track.enabled = false)), Azure Speech SDK still captures microphone audio. Even when I explicitly block microphone access in the browser settings, Azure Speech continues to detect microphone input.

I suspect the issue lies in:

using var audioConfig = AudioConfig.FromDefaultSpeakerOutput();

It seems like AudioConfig ignores the JavaScript constraints and pulls audio from all available sources, including the microphone. This behavior breaks the expected functionality, as my use case only requires system audio from the shared screen.

What I Have TriedDisabled microphone tracks in the JavaScript function managing screen sharing.Blocked microphone access in the browser settings.Experimented with AudioConfig options, such as FromStreamInput and FromDefaultSpeakerOutput.None of these approaches have successfully restricted AudioConfig to system audio only.

My QuestionsHow can I configure Azure Speech SDK's AudioConfig to exclusively capture system audio and completely ignore microphone input?Is there a better way to handle audio streams (e.g., passing audio streams from JavaScript to Blazor and then to Azure)?

Any guidance or suggestions would be highly appreciated.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>