Partager via


SpeechRecognizer.StopListeningAndProcessAudio() method

 

The StopListeningAndProcessAudio() method interrupts the current audio capture and finishes analysis on the captured audio data.

Syntax

public async void StopListeningAndProcessAudio();

Remarks

The StopListeningAndProcessAudio() method should only be called during the listening phase of the audio capture process. If called at other times, it will have no effect.

Creating a button or other UI to make the StopListeningAndProcessAudio() method available empowers users to end the listening phase without waiting for the SpeechRecognizer to identify the end of speech.

Example

The following example uses button click handlers to call the StopListeningAndProcessAudio() and RequestCancelOperation() methods, and handles the AudioCaptureStateChanged event for SpeechRecognizer SR. The AudioCaptureStateChanged event handler shows and hides the Stop and Cancel buttons based on their relevance to the current state of the speech recognition process.

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
    CancelButton.Visibility = Visibility.Collapsed;
    StopButton.Visibility = Visibility.Collapsed;
    SR.RequestCancelOperation();
}

private void StopButton_Click(object sender, RoutedEventArgs e)
{
    StopButton.Visibility = Visibility.Collapsed;
    SR.StopListeningAndProcessAudio();
}

private void SR_AudioCaptureStateChanged(SpeechRecognizer sender,
        SpeechRecognitionAudioCaptureStateChangedEventArgs args)
{
    switch (args.State)
    {
        case SpeechRecognizerAudioCaptureState.Canceled:
            StatusBar.Text = "Operation cancelled.";
            break;
        case SpeechRecognizerAudioCaptureState.Cancelling:
            StatusBar.Text = "Cancelling capture operation...";
            break;
        case SpeechRecognizerAudioCaptureState.Complete:
            StatusBar.Text = "Audio capture complete.";
            break;
        case SpeechRecognizerAudioCaptureState.Initializing:
            StatusBar.Text = "Initializing audio capture...";
            CancelButton.Visibility = Visibility.Visible;
            break;
        case SpeechRecognizerAudioCaptureState.Listening:
            StatusBar.Text = "Listening...";
            StopButton.Visibility = Visibility.Visible;
            break;
        case SpeechRecognizerAudioCaptureState.Thinking:
            StatusBar.Text = "Interpreting audio input...";
            VolumeBar.Text = string.Empty;
            StopButton.IsEnabled = false;
            break;
        default:
            StatusBar.Text = "Unknown capture state.";
            break;
    }
}
function CancelButton_Click(sender, e) {
    document.getElementById("CancelButton").style.display = 'none';
    document.getElementById("StopButton ").style.display = 'none';
    SR.RequestCancelOperation();
}

function StopButton_Click(sender, e) {
    document.getElementById("StopButton ").style.display = 'none';
    SR.StopListeningAndProcessAudio();
}

function SR_AudioCaptureStateChanged(sender, args) {
    var statusBar = document.getElementById("StatusBar ");
    var volumeBar = document.getElementById("VolumeBar ");
    var stopButton = document.getElementById("StopButton ");
    var cancelButton = document.getElementById("CancelButton ");

    switch (args.State) {
        case SpeechRecognizerAudioCaptureState.Canceled:
            statusBar.innerText = "Operation cancelled.";
            break;
        case SpeechRecognizerAudioCaptureState.Cancelling:
            statusBar.innerText = "Cancelling capture operation...";
            break;
        case SpeechRecognizerAudioCaptureState.Complete:
            statusBar.innerText = "Audio capture complete.";
            break;
        case SpeechRecognizerAudioCaptureState.Initializing:
            statusBar.innerText = "Initializing audio capture...";
            cancelButton.style.display = 'block';
            break;
        case SpeechRecognizerAudioCaptureState.Listening:
            statusBar.innerText = "Listening...";
            stopButton.style.display = 'block';
            break;
        case SpeechRecognizerAudioCaptureState.Thinking:
            statusBar.Text = "Interpreting audio input...";
            volumeBar.Text = "";
            stopButton.style.display = 'none';
            break;
        default:
            statusBar.Text = "Unknown capture state.";
            break;
    }
}

Requirements

Minimum Supported Client

Windows 8

Required Extensions

Bing.Speech

Namespace

Bing.Speech