網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2021-11-12 C/C++?Qt?StatusBar底部狀態欄應用教程_C 語言
- 2021-12-03 Go并發編程中sync/errGroup的使用_Golang
- 2022-07-30 SpringBoot的數據校驗(@Validated注解)、關于validation無法導入的問題解
- 2022-07-06 C語言中#pragma?once的作用_C 語言
- 2022-06-20 .NET?Core企業微信網頁授權登錄的實現_實用技巧
- 2022-07-09 Python如何通過變量ID得到變量的值_python
- 2022-09-13 本地使用Docker搭建go開發環境的全過程_Golang
- 2022-09-03 ahooks控制時機的hook實現方法_React
- 最近更新
-
- 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同步修改后的遠程分支