gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:07:54 25 4
gpt4 key购买 nike

这是我用来表达 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com