C#实现语音播报功能的示例详解

 更新时间:2024年02月01日 10:17:18   作者:wangyue4  
这篇文章主要为大家详细介绍了如何使用C#实现语音播报功能,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下

在C#中进行语音播报通常需要使用.NET Framework中的某个语音库或服务。一个常见的选择是使用System.Speech.Synthesis命名空间中的SpeechSynthesizer类,该类提供了文本到语音的转换功能。

以下是一个简单的示例,演示如何在C#中使用SpeechSynthesizer进行语音播报:

using System;
using System.Speech.Synthesis;
 
class Program
{
    static void Main()
    {
        // 创建SpeechSynthesizer实例
        using (SpeechSynthesizer synth = new SpeechSynthesizer())
        {
            // 设置语音合成引擎的声音
            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中不是默认支持的库。如果你的项目是基于.NET Core,请考虑使用其他第三方语音合成库,例如Microsoft.CognitiveServices.Speech SDK或其他可用的库。

使用 Cognitive Services Speech SDK 进行语音播报:

安装 Microsoft.CognitiveServices.Speech NuGet 包: 在你的项目中安装 Microsoft.CognitiveServices.Speech NuGet 包。你可以在 Visual Studio 中通过右键单击项目 -> 添加 -> NuGet 包管理器 -> 管理 NuGet 包来完成。

使用 Speech SDK 进行语音播报: 在代码中,你可以使用如下方式:

using System;
using Microsoft.CognitiveServices.Speech;
using System.Threading.Tasks;
 
class Program
{
    static async Task Main()
    {
        // 替换为你的 Cognitive Services Speech API 密钥和区域
        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 的实际密钥和区域。

这个示例使用了异步操作,因此 Main 方法声明为 async Task。请注意,使用云服务需要网络连接,并且可能会涉及使用费用,具体取决于你的使用情况。

知识补充

除了上文的方法,小编还为大家整理了其他C#实现语音播报的方法,希望对大家有所帮助

方法一

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();
    }

到此这篇关于C#实现语音播报功能的示例详解的文章就介绍到这了,更多相关C#语音播报内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#图像处理之边缘检测(Smoothed)的方法

    C#图像处理之边缘检测(Smoothed)的方法

    这篇文章主要介绍了C#图像处理之边缘检测(Smoothed)的方法,使用自定义smoothed算子实现对图像边缘检测的功能,需要的朋友可以参考下
    2015-04-04
  • C#使用Dispose模式实现手动对资源的释放

    C#使用Dispose模式实现手动对资源的释放

    这篇文章主要介绍了C#使用Dispose模式实现手动对资源的释放,涉及C#采用Dispose模式操作资源的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • 验证码的三个常见漏洞和修复方法

    验证码的三个常见漏洞和修复方法

    这篇文章主要介绍了验证码的三个常见漏洞和修复方法,本文讲解了把验证码存储在Cookie中、没有进行非空判断、没有及时销毁验证码三个常见问题和解决方法,需要的朋友可以参考下
    2015-03-03
  • C#对DataTable里数据排序的方法

    C#对DataTable里数据排序的方法

    在日常开发过程中,有一个DataTable集合,里面有很多字段,现在要求针对某一列进行排序,如果该列为数字的话,进行ASC即可实现,但是该字段类型为string,此时排序就有点不正确了
    2013-11-11
  • 一文教你如何使用C#开发一个Windows后台服务

    一文教你如何使用C#开发一个Windows后台服务

    这篇文章主要为大家详细介绍了如何基于C#实现Windows服务的创建、安装、启动、停止和卸载,并展示具体的代码示例和操作步骤,需要的小伙伴可以了解下
    2025-08-08
  • C#实现公式计算验证码的示例详解

    C#实现公式计算验证码的示例详解

    现在很多的平台已经不使用普通的数字、字母等验证码了,取而代之的是拼图类、选图类、旋转类或者计算类的验证码。本文将利用C#实现一个公式计算验证码,感兴趣的可以了解一下
    2022-10-10
  • C# WPF实现页面跳转的两种方法介绍

    C# WPF实现页面跳转的两种方法介绍

    在 C# WPF 中,页面跳转通常有两种主要方式:使用 NavigationWindow+Page或在 Window 中切换 UserControl,下面我们来看看具体实现方法吧
    2025-10-10
  • C#如何调用Python生成的pyd文件

    C#如何调用Python生成的pyd文件

    这篇文章主要介绍了C#如何调用Python生成的pyd文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • C# Assembly.Load案例详解

    C# Assembly.Load案例详解

    这篇文章主要介绍了C# Assembly.Load案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Winform在DataGridView中显示图片

    Winform在DataGridView中显示图片

    本文主要介绍在DataGridView如何显示图片,简单实用,需要的朋友可以参考下。
    2016-05-05

最新评论