public class VoiceControl
{
private SpeechSynthesizer synthesizer;
private Thread speechThread;
private bool isPlaying;
public VoiceControl()
{
synthesizer = new SpeechSynthesizer();
isPlaying = false;
}
/// <summary>
/// 设置语音的速度
/// </summary>
/// <param name="rate">速度值,范围从-10到10</param>
public void SetRate(int rate)
{
synthesizer.Rate = rate;
}
/// <summary>
/// 设置语音的音量
/// </summary>
/// <param name="volume">音量值,范围从0到100</param>
public void SetVolume(int volume)
{
synthesizer.Volume = volume;
}
/// <summary>
/// 选择语音合成的声音
/// </summary>
/// <param name="voiceName">声音的名称</param>
public void SelectVoice(string voiceName)
{
synthesizer.SelectVoice(voiceName);
}
/// <summary>
/// 播放指定文本的语音
/// </summary>
/// <param name="text">要播放的文本</param>
public void Speak(string text)
{
synthesizer.Speak(text);
}
/// <summary>
/// 播放指定文本的语音,重复指定次数
/// </summary>
/// <param name="text">要播放的文本</param>
/// <param name="repeatCount">重复次数</param>
public void Speak(string text, int repeatCount)
{
isPlaying = true;
speechThread = new Thread(() => PlaySpeech(text, repeatCount));
speechThread.Start();
//for (int i = 0; i < repeatCount; i++)
//{
// synthesizer.Speak(text);
//}
}
/// <summary>
/// 循环播放指定文本的语音,直到有人处理为止
/// </summary>
/// <param name="text">要播放的文本</param>
public void SpeakUntilHandled(string text)
{
while (true)
{
synthesizer.Speak(text);
// 检查是否有用户输入或其他处理逻辑
// 如果有处理逻辑,可以使用break跳出循环
// 例如,检查用户按下了某个特定的键或触发了某个事件
// 如果有处理逻辑,使用break跳出循环,停止语音播放
// 如果没有处理逻辑,循环将继续播放语音
}
}
/// <summary>
/// 循环播放指定文本的语音,直到手动停止
/// </summary>
/// <param name="text">要播放的文本</param>
public void SpeakInLoop(string text)
{
isPlaying = true;
speechThread = new Thread(() => PlaySpeechInLoop(text));
speechThread.Start();
}
private void PlaySpeech(string text, int repeatCount)
{
for (int i = 0; i < repeatCount; i++)
{
if (isPlaying)
{
synthesizer.Speak(text);
}
else
{
break;
}
}
isPlaying = false;
}
private void PlaySpeechInLoop(string text)
{
while (true)
{
if (isPlaying)
{
synthesizer.Speak(text);
}
else
{
break;
}
}
}
/// <summary>
/// 停止语音播放
/// </summary>
public void Stop()
{
if (isPlaying)
{
synthesizer.SpeakAsyncCancelAll();
speechThread.Abort();
isPlaying = false;
}
}
/// <summary>
/// 停止语音播放
/// </summary>
public void StopAll()
{
synthesizer.SpeakAsyncCancelAll();
}
}
//窗体调用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
private VoiceControl voiceControl;
public MainForm()
{
InitializeComponent();
voiceControl = new VoiceControl();
}
private void button1_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
//voiceControl.SpeakUntilHandled(text);
voiceControl.SpeakInLoop(text);
}
private void btnStop_Click(object sender, EventArgs e)
{
voiceControl.Stop();
}
}
}

1624

被折叠的 条评论
为什么被折叠?



