SpeechSynthesizer.SpeakAsyncCancelAll()不会取消

时间:2014-09-10 15:04:18

标签: vb.net text-to-speech speechsynthesizer

我的表单上有一个标记为Speak的按钮,当用户点击它synth.SpeakAsync(Textbox.text)时,该按钮会将其文本更改为“Stop Speaking”。

然而,当用户点击停止说话时,从而触发synth.speakAsyncCancelAll()它不会取消语音中文。

这是我的代码:

Private Sub btnSpeak_Click(sender As System.Object, e As System.EventArgs) Handles btnSpeak.Click
    If RTextBox.Text = "" Then Exit Sub

    Dim synth As New SpeechSynthesizer
    synth.Rate = -2

    If btnSpeak.Text = "Stop Speaking" Then
        btnSpeak.Text = "Speak"
        synth.SpeakAsyncCancelAll()

    ElseIf btnSpeak.Text = "Speak" Then
        btnSpeak.Text = "Stop Speaking"
        synth.SpeakAsync(RTextBox.Text)
    End If

2 个答案:

答案 0 :(得分:1)

每次单击该按钮时,都会创建一个新的语音合成器。将dim synth作为按钮点击事件中的新语音合成器。这样它每次都会引用相同的合成器,然后应该触发语音取消事件

答案 1 :(得分:0)

SpeakAsyncCancelAll()会在被调用后引发SpeakCompleted事件,您可以使用以下代码收听它:

Imports System.Speech.Synthesis

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim synthesizer As New SpeechSynthesizer()
        AddHandler synthesizer.SpeakCompleted, AddressOf speakCompleted
    End Sub

    Private Sub speakCompleted(ByVal sender As Object, ByVal e As SpeakCompletedEventArgs)
        Dim cancelled = e.Cancelled
    End Sub
End Class