網站首頁 編程語言 正文
本文實例為大家分享了C#實現語音播報功能的具體代碼,供大家參考,具體內容如下
環境:
window10
vs2019 16.5.5
.netframework4.5
一、關于語音播報
語音播報的功能屬于操作系統自帶的。win7和win10都自帶,部分win7閹割版系統沒有這項功能會導致運行報錯:
檢索 COM 類工廠中 CLSID 為 {D9F6EE60-58C9-458B-88E1-2F908FD7F87C} 的組件失敗,原因是出現以下錯誤: 80040154 沒有注冊類 (異常來自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。
查看自己電腦是否支持語音播報功能,可以參考如下:
二、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(); ? ? ? ? } ? ? } }
運行后,帶好耳機,查看效果:
原文鏈接:https://blog.csdn.net/u010476739/article/details/106192826
相關推薦
- 2022-04-23 uniapp封裝本地存儲處理數據的方法和具體使用
- 2023-10-15 [element-ui] el-input 不能正常輸入,輸入了也不能刪除的問題
- 2022-12-07 C++11?成員函數作為回調函數的使用方式_C 語言
- 2022-05-12 C語言的數組指針與函數指針詳解_C 語言
- 2023-01-26 Python編程之順序執行與程序的主入口詳解_python
- 2022-07-08 python中的type,元類,類,對象用法_python
- 2022-11-22 XML實體注入深入理解_XML示例
- 2023-04-21 深入理解Python中__init__.py文件_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支