Share via


SpeechRecognizer.RequestCancelOperation() method

 

The RequestCancelOperation() method interrupts speech recognition and returns control to the caller. This method can be called at any point in the speech recognition process.

Syntax

public void RequestCancelOperation();

Remarks

When the RequestCancelOperation() method is called, the SpeechRecognizer first fires the AudioCaptureStateChanged event with a State of SpeechRecognizerAudioCaptureState.Cancelling. It then discards all audio, interpreted text, and other data from the current session. When finished, it fires the AudioCaptureStateChanged event a second time with a state of SpeechRecognizerAudioCaptureState.Cancelled, and then returns control to the caller.

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