这篇文章主要为大家详细介绍了C#实现语音播报功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文分享C#实现语音播报功能的具体代码,供大家参考,具体内容如下
环境:
windows 10
vs 2019 16 . 5 . 5
. net framework 4.5
一、关于语音播报
语音播报的功能属于操作系统。Win7和win10自带,win7的一些阉割版系统没有这个功能,会导致运行错误:
由于以下错误,在COM类工厂中检索CLSID为{ d 9 F6 ee 60-58c 9-458 b-88e 1-2 f 908 FD 7 f 87 c }的组件失败:80040154没有注册的类(异常来自HRESULT:0x 80040154(regdb _ e _ class notreg))。
看看你的电脑是否支持语音播报功能。请参考以下内容:
二、C#代码
直接创建一个新的控制台程序并添加System.Speech.dll引用:
代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Speech.Synthesis;using System.Text;using System.Threading.Tasks;namespace ConsoleApp9{? ? class Program? ? {? ? ? ? static void Main(string[] args)? ? ? ? {? ? ? ? ? ? SpeechSynthesizer speech = new SpeechSynthesizer();? ? ? ? ? ? Console.Write("请输入文字:");? ? ? ? ? ? string str = Console.ReadLine();? ? ? ? ? ? try? ? ? ? ? ? {? ? ? ? ? ? ? ? if (string.IsNullOrEmpty(str))? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? speech.Speak("请输入文字");? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? speech.Speak(str);? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? catch (Exception ex)? ? ? ? ? ? {? ? ? ? ? ? ? ? Console.WriteLine($"报错:{ex?.Message}");? ? ? ? ? ? }? ? ? ? ? ? Console.WriteLine("ok");? ? ? ? ? ? Console.ReadLine();? ? ? ? }? ? }}
跑步结束后,带上耳机,检查效果:
这就是本文的全部内容。希望对大家的学习有帮助,也希望大家能支持一下搜源网。
精彩评论