实时语音播报

using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ManagementSystem.Project.Tools
{
/// <summary>
/// 语音播报帮助类
/// </summary>
public class SpeakHelper
{
//语音同步器
public static SpeechSynthesizer Synth;
//创建并控制线程
private static Thread _thread;
//文本内容
private static string _contentThread;

/// <summary>
/// 播放
/// </summary>
public static void SpeakContentThread()
{
try
{
if (Synth != null)
{
//释放 SpeechSynthesizer 在会话期间使用的对象并释放资源
Synth.Dispose();
}
//实例化
Synth = new SpeechSynthesizer();
Synth.Rate = -1;
//执行文本内容
Synth.Speak(_contentThread);
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 停止播放
/// </summary>
public static void StopSpeak()
{
try
{
if (Synth != null)
{
//释放 SpeechSynthesizer 在会话期间使用的对象并释放资源
Synth.Dispose();
}

if (_thread != null)
{
//终止线程
_thread.Abort();
}
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 接受文本内容
/// </summary>
/// <param name="content"></param>
public static void SpeekContent(string content)
{
//文本内容
_contentThread = content;
//执行播放
_thread = new Thread(SpeakContentThread);
//设置为后台线程
_thread.IsBackground = true;
//开始线程
_thread.Start();
}

}
}

猜你喜欢

转载自www.cnblogs.com/qyq0323/p/11765265.html
今日推荐