System.Speech.Synthesis的事件信息中的流编号在哪里?

tmi*_*hty 6 .net vb.net speech-synthesis speechsynthesizer

可以让SpeechSynthesizer以异步方式讲文本,例如:

Private WithEvents _Synth As New SpeechSynthesizer

Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    If e.KeyCode = Keys.Enter Then
        _Synth.SpeakAsync(New Prompt(Me.TextBox1.Text))
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

SpeechSynthesizer产生的事件使我们能够分辨出计算机语音在说什么。

例如,您可以通过选择以下字符来可视化语音输出:

Private Sub _Synth_SpeakProgress(sender As Object, e As SpeakProgressEventArgs) Handles _Synth.SpeakProgress

    Me.TextBox1.SelectionStart = e.CharacterPosition
    Me.TextBox1.SelectionLength = e.CharacterCount

End Sub
Run Code Online (Sandbox Code Playgroud)

但是,当SpeakAsync反复被调用时(例如,当我们告诉SpeechSyntesizer他们当前正在讲话时说相同的文本时),语音请求会排队,然后SpeechSynthesizer一一播放。

但是,我无法找出合成器当前正在发出哪个请求。在SpeakProgressEventArgs不透露这个

使用SAPI5,事件提供了StreamNumber

Parameters
StreamNumber
    The stream number which generated the event. When a voice enqueues more than one stream by speaking asynchronously, the stream number is necessary to associate an event with the appropriate stream.
Run Code Online (Sandbox Code Playgroud)

使用此StreamNumber,您总是可以分辨出SpeechSynthesizer只是在播放/讲话。

System.Speech.Synthesis实现是SAPI5实现的现代版本。

但是,我只是找不到StreamNumber指示符或类似信息。

System.Speech.Synthesis仅提供有关正在发生的所有事情的信息,因此,它不太可能不提供仅在处理哪些请求的信息。

如何找到它?

TnT*_*nMn 1

为了澄清我关于使用提示类来保存您需要的任何标识状态的评论,请考虑以下内容,其中Prompt保存对源的引用TextBox

Imports System.Speech.Synthesis
Public Class MyPrompt : Inherits Prompt
    Private tbRef As WeakReference(Of TextBox)

    Public Sub New(textBox As TextBox)
        MyBase.New(textBox.Text)
        ' only hold a weak reference to the TextBox
        ' to avoid any disposal issues
        tbRef = New WeakReference(Of TextBox)(textBox)
    End Sub

    Public ReadOnly Property SourceTextBox As TextBox
        Get
            Dim ret As TextBox = Nothing
            tbRef.TryGetTarget(ret)
            Return ret
        End Get
    End Property
End Class
Run Code Online (Sandbox Code Playgroud)

现在你的原始代码可以写成:

Imports System.Speech.Synthesis

Public Class Form1
    Private WithEvents _Synth As New SpeechSynthesizer

    Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = Keys.Enter Then
            ' use a custom prompt to store the TextBox
            _Synth.SpeakAsync(New MyPrompt(Me.TextBox1))
        End If
    End Sub

    Private Sub _Synth_SpeakProgress(sender As Object, e As SpeakProgressEventArgs) Handles _Synth.SpeakProgress
        Dim mp As MyPrompt = TryCast(e.Prompt, MyPrompt)
        If mp IsNot Nothing Then
            Dim tb As TextBox = mp.SourceTextBox
            If tb IsNot Nothing Then
                ' set the selection in the source TextBox
                tb.SelectionStart = e.CharacterPosition
                tb.SelectionLength = e.CharacterCount
            End If
        End If
    End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

编辑:

OP 希望将此与SpeakSsmlAsync 方法一起使用。这本身是不可能的,因为该方法Prompt使用Prompt(String, SynthesisTextFormat) 构造函数Prompt创建一个基础,并在调用后返回创建的基础SpeechSynthesizer.SpeakAsync(created_prompt)

下面是一个派生Prompt类,它接受 ssml 字符串或PromptBuilder 实例以及整数标识符。新版本的 MyPrompt 使用 ssml 和整数标识符。

Imports System.Speech.Synthesis

Public Class MyPromptV2 : Inherits Prompt
    Public Sub New(ssml As String, identifier As Int32)
        MyBase.New(ssml, SynthesisTextFormat.Ssml)
        Me.Identifier = identifier
    End Sub

    Public Sub New(builder As PromptBuilder, identifier As Int32)
        MyBase.New(builder)
        Me.Identifier = identifier
    End Sub

    Public ReadOnly Property Identifier As Int32
End Class
Run Code Online (Sandbox Code Playgroud)

...

Imports System.Speech.Synthesis

Public Class Form1
    Private WithEvents _Synth As New SpeechSynthesizer

    Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = Keys.Enter Then
            ' build some ssml from the text
            Dim pb As New PromptBuilder
            pb.AppendText(TextBox1.Text)
            ' use ssml and and integer
            _Synth.SpeakAsync(New MyPrompt(pb.ToXml, 10))
            ' or 
            '_Synth.SpeakAsync(New MyPrompt(pb, 10))
        End If
    End Sub

    Private Sub _Synth_SpeakProgress(sender As Object, e As SpeakProgressEventArgs) Handles _Synth.SpeakProgress
        Dim mp As MyPromptV2 = TryCast(e.Prompt, MyPromptV2)
        If mp IsNot Nothing Then
            Select Case mp.Identifier
                Case 10
                    TextBox1.SelectionStart = e.CharacterPosition
                    TextBox1.SelectionLength = e.CharacterCount
            End Select
        End If
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)