c# - 如何中断应用程序中文本语音合成的播放?

标签 c# speech speechsynthesizer

这是我用来表达 richTextBox 的代码。我的问题是,在播放文本时我无法单击任何内容。我什至无法停止玩。我该如何解决这个问题?有什么方法可以通过单击按钮来停止播放吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Speech.Synthesis;

namespace Merger
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer tell = new SpeechSynthesizer();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;

            tell.Rate = trackBar1.Value;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            tell.Volume = 100;
            tell.Speak(richTextBox1.SelectedText);
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            tell.Rate = trackBar1.Value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tell.SpeakAsyncCancelAll();
        }
    }
}

最佳答案

问题在于 Speak() 方法是同步的,因此它将锁定您所在的线程。假设您位于单个线程上,该线程将是 UI 线程,从而锁定您正在执行的任何操作。

您可能最好使用不同的线程来 Speak(),这不会锁定您当前的 (UI) 线程。

SpeechSynthesizer.Speak Method (String) - MSDN

或者您可以使用 SpeechAsync 方法,该方法将异步执行!

SpeechSynthesizer.SpeakAsync Method (String) - MSDN

关于c# - 如何中断应用程序中文本语音合成的播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22641973/

相关文章:

c# - SpeechSynthesizer.SelectVoice() 失败并显示 "No matching voice is installed or the voice was disabled"

c# - 如何在 aspx 中运行 if 语句?

c# - 如果我想实现这个线程概念,我应该如何使用 TPL?

c# - 无法从子窗体访问 MDI 父窗体的标签文本

javascript - javascript node.js 的语音识别

c# - ASP.NET MVC 中的超快速文本转语音(WAV -> MP3)

c# - 如何在C# winform中一行显示listview中的文件?

audio - 不同语言(非英语)的PESQ,STOI得分(语音质量)

api - 谷歌云语音 API 词提示