作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我用来表达 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 方法,它将以异步方式执行!
关于c# - 如何在我的应用程序中中断文本到语音合成的播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22641973/
我是一名优秀的程序员,十分优秀!