C#實(shí)現(xiàn)語(yǔ)音播報(bào)功能的示例詳解
在C#中進(jìn)行語(yǔ)音播報(bào)通常需要使用.NET Framework中的某個(gè)語(yǔ)音庫(kù)或服務(wù)。一個(gè)常見(jiàn)的選擇是使用System.Speech.Synthesis命名空間中的SpeechSynthesizer類,該類提供了文本到語(yǔ)音的轉(zhuǎn)換功能。
以下是一個(gè)簡(jiǎn)單的示例,演示如何在C#中使用SpeechSynthesizer進(jìn)行語(yǔ)音播報(bào):
using System;
using System.Speech.Synthesis;
class Program
{
static void Main()
{
// 創(chuàng)建SpeechSynthesizer實(shí)例
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// 設(shè)置語(yǔ)音合成引擎的聲音
synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
// 播報(bào)文本
string textToSpeak = "Hello, this is a test. I am speaking in C#.";
synth.Speak(textToSpeak);
Console.WriteLine("Speech completed.");
}
}
}
請(qǐng)確保在你的項(xiàng)目中引用了System.Speech程序集。你可以在Visual Studio中通過(guò)右鍵單擊項(xiàng)目 -> 添加 -> 引用 -> 程序集 -> 框架 -> System.Speech 來(lái)添加引用。
注意:System.Speech.Synthesis在.NET Core中不是默認(rèn)支持的庫(kù)。如果你的項(xiàng)目是基于.NET Core,請(qǐng)考慮使用其他第三方語(yǔ)音合成庫(kù),例如Microsoft.CognitiveServices.Speech SDK或其他可用的庫(kù)。
使用 Cognitive Services Speech SDK 進(jìn)行語(yǔ)音播報(bào):
安裝 Microsoft.CognitiveServices.Speech NuGet 包: 在你的項(xiàng)目中安裝 Microsoft.CognitiveServices.Speech NuGet 包。你可以在 Visual Studio 中通過(guò)右鍵單擊項(xiàng)目 -> 添加 -> NuGet 包管理器 -> 管理 NuGet 包來(lái)完成。
使用 Speech SDK 進(jìn)行語(yǔ)音播報(bào): 在代碼中,你可以使用如下方式:
using System;
using Microsoft.CognitiveServices.Speech;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
// 替換為你的 Cognitive Services Speech API 密鑰和區(qū)域
var apiKey = "YourSpeechApiKey";
var region = "YourSpeechApiRegion";
var config = SpeechConfig.FromSubscription(apiKey, region);
using var synthesizer = new SpeechSynthesizer(config);
// 播報(bào)文本
var textToSpeak = "Hello, this is a test. I am speaking in .NET Core.";
var result = await synthesizer.SpeakTextAsync(textToSpeak);
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine("Speech completed.");
}
else
{
Console.WriteLine($"Speech synthesis failed: {result.Reason}");
}
}
}
確保替換 YourSpeechApiKey 和 YourSpeechApiRegion 為你的 Cognitive Services Speech API 的實(shí)際密鑰和區(qū)域。
這個(gè)示例使用了異步操作,因此 Main 方法聲明為 async Task。請(qǐng)注意,使用云服務(wù)需要網(wǎng)絡(luò)連接,并且可能會(huì)涉及使用費(fèi)用,具體取決于你的使用情況。
知識(shí)補(bǔ)充
除了上文的方法,小編還為大家整理了其他C#實(shí)現(xiàn)語(yǔ)音播報(bào)的方法,希望對(duì)大家有所幫助
方法一
1、首先要安裝語(yǔ)音包Microsoft Speech SDK 5.1
2、引用 Interop.SpeechLib.dll
3、然后以下代碼即可
SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice voice = new SpVoice();
voice.Rate = 1;//語(yǔ)速
voice.Volume = 100;//音量
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);//Item(0)中文、Item(3)英文
voice.Speak("語(yǔ)音播報(bào)", flag);方法二
List<string> ls_speack = new List<string>();
public void Speaking()
{
Task task = new Task(() =>
{
while (true)
{
Thread.Sleep(100);
if (ls_speack.Count == 0)
{
continue;
}
SpeechSynthesizer speech = new SpeechSynthesizer();
speech.Volume = 100; //音量
CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;
InstalledVoice neededVoice = speech.GetInstalledVoices(keyboardCulture).FirstOrDefault();
if (neededVoice == null)
{
//say = "未知的操作";
}
else
{
speech.SelectVoice(neededVoice.VoiceInfo.Name);
}
for (int k = 0; k < ls_speack.Count; k++)
{
Thread.Sleep(100);
speech.Speak(ls_speack[k]);
}
ls_speack = new List<string>();
}
});
task.Start();
}
public static void Speaking(string saying)
{
string say = saying;
Task task = new Task(() =>
{
SpeechSynthesizer speech = new SpeechSynthesizer();
speech.Volume = 100; //音量
CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;
InstalledVoice neededVoice = speech.GetInstalledVoices(keyboardCulture).FirstOrDefault();
if (neededVoice == null)
{
say = "未知的操作";
}
else
{
speech.SelectVoice(neededVoice.VoiceInfo.Name);
}
speech.Speak(say);
});
task.Start();
}
到此這篇關(guān)于C#實(shí)現(xiàn)語(yǔ)音播報(bào)功能的示例詳解的文章就介紹到這了,更多相關(guān)C#語(yǔ)音播報(bào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中計(jì)數(shù)排序算法的原理及實(shí)現(xiàn)
計(jì)數(shù)排序是一種線性時(shí)間復(fù)雜度的排序方法,主要通過(guò)統(tǒng)計(jì)元素出現(xiàn)的次數(shù)實(shí)現(xiàn)排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
C#中比較兩個(gè)List是否相等的常見(jiàn)方法
在?C#?里,比較兩個(gè)?List?是否相等,需要考慮多個(gè)方面,例如列表中的元素順序、元素本身是否相等,下面介紹幾種常見(jiàn)的比較方法,需要的朋友可以參考下2025-04-04
C# FileStream實(shí)現(xiàn)大文件復(fù)制
這篇文章主要為大家詳細(xì)介紹了C# FileStream實(shí)現(xiàn)大文件復(fù)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05

