当前位置: 首页 > news >正文

C#实现普通的语音播报

Windows有文字转语音功能,C#提供了调用的类库Interop.SpeechLib.dll

使用方法很简单,在你的项目中添加Interop.SpeechLib.dll引用,在类中引用:

using SpeechLib;

这里提供一个CVoice类 帮助实现语音播报

 public class CVoice{private static Speach mysp = Speach.instance();private static SoundPlayer _voiceplayer = new SoundPlayer();private static Thread threadplay;private static string _sound;public static string SoundfileName{get { return _sound; }}private static string _soundtext;public static string SoundText{get { return CVoice._soundtext; }}public static void Playfile(string soundfile){_sound = soundfile;threadplay = new Thread(mysoundplay);threadplay.Start();}public static void Play(string soundtext){try{_soundtext = soundtext.Replace("重", "虫");  //解决都“zhong”的问题mysp.AnalyseSpeak(soundtext);}catch{}}public static void RePlay() //播放最后一次的语音{mysp.AnalyseSpeak(_soundtext);}public static void Pause() //播放最后一次的语音{mysp.Pause();}public static void Continue() //播放最后一次的语音{mysp.Continue();}public static void Stop(){//if (threadplay.ThreadState == ThreadState.Running)//{//    threadplay.Abort();//}mysp.Stop();}private static void mysoundplay(){try{_voiceplayer.SoundLocation = _sound;_voiceplayer.Play();}catch{}}public static Speach Speech{get{return mysp;}}}public class Speach{private static Speach _Instance = null;private SpeechLib.SpVoiceClass voice = null;private Speach(){BuildSpeach();}public static Speach instance(){if (_Instance == null)_Instance = new Speach();return _Instance;}public void SelectVoice(int index){try{voice.Voice = voice.GetVoices("", "").Item(index);}catch{}}private void BuildSpeach(){if (voice == null) voice = new SpVoiceClass();SelectVoice(0);}public int Volume{get{return voice.Volume;}set{voice.SetVolume((ushort)(value));}}public int Rate{get{return voice.Rate;}set{voice.SetRate(value);}}private void Speak(string strSpeack){try{int nn = voice.Speak(strSpeack.Replace("重", "虫"), SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);Console.WriteLine(nn);}catch (Exception err){throw (new Exception("发生一个错误:" + err.Message));}}public void AnalyseSpeak(string strSpeak){Speak(strSpeak);}public void Stop(){voice.Speak(string.Empty, SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);}public void Pause(){voice.Pause();}public void Continue(){voice.Resume();}}在需要的地方只需要CVoice.Play(“语音播报内容”);
需要dll文件的私聊我!
http://www.lryc.cn/news/128354.html

相关文章:

  • django中实现事务的几种方式
  • 【es6】具名组匹配
  • 自然语言处理技术:NLP句法解析树与可视化方法
  • flinksql报错 Cannot determine simple type name “org“
  • e.target.value和 binding.value 区别
  • 软链接与exec进程替换运行路径问题
  • 【Go】锁相关
  • git环境超详细配置说明
  • 使用阿里云服务器搭建PostgreSQL主从架构图文流程
  • Linux的基本权限(文件,目录)
  • 网络编程(12): TCP重传、滑动窗口、流量控制、拥塞控制
  • Docker安装RabbitMQ服务端
  • vueuse常用方法
  • 选择大型语言模型自定义技术
  • 算法概述-Java常用算法
  • CCLINK转MODBUS-TCP网关cclink通讯接线图 终端电阻
  • 香蕉派 BPI-P2 Pro采用RK3308芯片,512M内存,8G存储,支持PoE供电
  • 「隐语小课」拆分学习之“水平拆分学习”
  • WPF--关于Action事件小结
  • 创建一个 React+Typescript 项目
  • Java课题笔记~ 数据提交的方式
  • VUE3给页面添加按钮事件
  • 基于centos7.9通过nginx实现负载均衡以及反向代理
  • 前端原生写自定义旋转变换轮播图
  • linux tomcat server.xml 项目访问路径变更不生效
  • 介绍原型模式:快速构建和复制对象的设计模式
  • Unity的TimeScale的影响范围分析
  • 爬虫逆向实战(五)--猿人学第三题
  • [虚幻引擎] UE使用虚拟纹理在模型上显示挖空效果
  • vue3中reactive和ref的比较