日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現語音播報功能_C#教程

作者:jackletter ? 更新時間: 2022-05-29 編程語言

本文實例為大家分享了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

欄目分類
最近更新