C#實現(xiàn)語音播報功能的示例詳解
在C#中進(jìn)行語音播報通常需要使用.NET Framework中的某個語音庫或服務(wù)。一個常見的選擇是使用System.Speech.Synthesis命名空間中的SpeechSynthesizer類,該類提供了文本到語音的轉(zhuǎn)換功能。
以下是一個簡單的示例,演示如何在C#中使用SpeechSynthesizer進(jìn)行語音播報:
using System;
using System.Speech.Synthesis;
class Program
{
static void Main()
{
// 創(chuàng)建SpeechSynthesizer實例
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// 設(shè)置語音合成引擎的聲音
synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
// 播報文本
string textToSpeak = "Hello, this is a test. I am speaking in C#.";
synth.Speak(textToSpeak);
Console.WriteLine("Speech completed.");
}
}
}
請確保在你的項目中引用了System.Speech程序集。你可以在Visual Studio中通過右鍵單擊項目 -> 添加 -> 引用 -> 程序集 -> 框架 -> System.Speech 來添加引用。
注意:System.Speech.Synthesis在.NET Core中不是默認(rèn)支持的庫。如果你的項目是基于.NET Core,請考慮使用其他第三方語音合成庫,例如Microsoft.CognitiveServices.Speech SDK或其他可用的庫。
使用 Cognitive Services Speech SDK 進(jìn)行語音播報:
安裝 Microsoft.CognitiveServices.Speech NuGet 包: 在你的項目中安裝 Microsoft.CognitiveServices.Speech NuGet 包。你可以在 Visual Studio 中通過右鍵單擊項目 -> 添加 -> NuGet 包管理器 -> 管理 NuGet 包來完成。
使用 Speech SDK 進(jìn)行語音播報: 在代碼中,你可以使用如下方式:
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);
// 播報文本
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 的實際密鑰和區(qū)域。
這個示例使用了異步操作,因此 Main 方法聲明為 async Task。請注意,使用云服務(wù)需要網(wǎng)絡(luò)連接,并且可能會涉及使用費用,具體取決于你的使用情況。
知識補充
除了上文的方法,小編還為大家整理了其他C#實現(xiàn)語音播報的方法,希望對大家有所幫助
方法一
1、首先要安裝語音包Microsoft Speech SDK 5.1
2、引用 Interop.SpeechLib.dll
3、然后以下代碼即可
SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice voice = new SpVoice();
voice.Rate = 1;//語速
voice.Volume = 100;//音量
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);//Item(0)中文、Item(3)英文
voice.Speak("語音播報", 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#實現(xiàn)語音播報功能的示例詳解的文章就介紹到這了,更多相關(guān)C#語音播報內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文教你如何使用C#開發(fā)一個Windows后臺服務(wù)
這篇文章主要為大家詳細(xì)介紹了如何基于C#實現(xiàn)Windows服務(wù)的創(chuàng)建、安裝、啟動、停止和卸載,并展示具體的代碼示例和操作步驟,需要的小伙伴可以了解下2025-08-08
C# WPF實現(xiàn)頁面跳轉(zhuǎn)的兩種方法介紹
在 C# WPF 中,頁面跳轉(zhuǎn)通常有兩種主要方式:使用 NavigationWindow+Page或在 Window 中切換 UserControl,下面我們來看看具體實現(xiàn)方法吧2025-10-10

