【问题标题】:How can I interrupt playing of text to speech synthesis in my application?如何在我的应用程序中中断文本到语音合成的播放?
【发布时间】:2014-03-25 17:24:23
【问题描述】:

这是我用来朗读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();
        }
    }
}

【问题讨论】:

    标签: c# speech speechsynthesizer


    【解决方案1】:

    问题是Speak() 方法是同步的,所以它会锁定你所在的线程。假设你在一个线程上,那将是 UI 线程,从而锁定你正在做的任何事情。

    您可能会更好地使用与Speak() 不同的线程,它不会锁定您当前的 (UI) 线程。

    SpeechSynthesizer.Speak Method (String) - MSDN

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

    SpeechSynthesizer.SpeakAsync Method (String) - MSDN

    【讨论】:

    • 您可以通过在调用 Speak() 的线程上调用 Thread.Abort() 来停止语音。
    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多