LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 如何实现文本语音自动播报

admin
2024年6月28日 11:20 本文热度 439

前言

在项目中,偶尔会使用语音播报一些信息,提醒异常或下一步的操作。如在扫描某个商品后,使用语音提示将其放在一号位等。在 C# 中如何实现将文本转为语音并播报,通过本文来了解其中的一种实现方式。

实现

.NET Framework 提供了 System.Speech 库,通过它实现使用 Windows 系统的文字转语音的播报功能。较为常见实现方式是使用 SpeechSynthesizer 类,也可以使用第三方语音合成库。当然对于.NET 5 以上是支持SpeechSynthesizer 类的,需要考虑使用其他第三方语音合成库。
使用此法实现很简单,在项目中添加 System.Speech 引用,然后可以使用 SpeechSynthesizer 实现文本的语音播报。

示例代码一:简单应用

using System.Globalization;

using System.Linq;

using System.Speech.Synthesis;

using System.Threading.Tasks;

namespace Fountain.WinConsole.TextToSpeech

{

    /// <summary>

    /// 文本转语音

    /// </summary>

    public class TextToSpeech

    {

        /// <summary>

        /// 播报

        /// </summary>

        /// <param name="rate">设置朗读频率 [范围  -10 至 10] </param>

        /// <param name="volume">设置朗读音量 [范围 0 至 100] </param>

        /// <param name="speektext">播报文本</param>

        public static void Speaking(int rate, int volume, string speektext)

        {

            Task task = new Task(() =>

            {

                SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();

                speechSynthesizer.Volume = volume; 

                speechSynthesizer.Rate = rate;

                InstalledVoice installedVoice = speechSynthesizer.GetInstalledVoices(CultureInfo.CurrentCulture).FirstOrDefault();

                if (installedVoice != null)

                {

                    speechSynthesizer.SelectVoice(installedVoice.VoiceInfo.Name);

                    speechSynthesizer.Speak(speektext);

                }

            });

            task.Start();

        }

    }

}


using System;

namespace Fountain.WinConsole.TextToSpeech

{

    internal class Program

    {

        static void Main(string[] args)

        {

            TextToSpeech.Speaking(4,100,"欢迎关注dotNet开发技术分享");

            Console.ReadKey();

        }

    }

}

示例代码二:文本转音频文件


using System.Globalization;

using System.Linq;

using System.Speech.Recognition;

using System.Speech.Synthesis;

using System.Threading.Tasks;

namespace Fountain.WinConsole.TextToSpeech

{

    /// <summary>

    /// 文本转语音

    /// </summary>

    public class TextToSpeech

    {

        /// <summary>

        /// 文本转语音文件

        /// </summary>

        /// <param name="rate">设置朗读频率 [范围  -10 至 10] </param>

        /// <param name="volume">设置朗读音量 [范围 0 至 100] </param>

        /// <param name="speektext">播报文本</param>

        public static void SpeakingToFile(int rate, int volume, string speektext)

        {

            try

            {

                SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();

                speechSynthesizer.Volume = volume;

                speechSynthesizer.Rate = rate;

                InstalledVoice installedVoice = speechSynthesizer.GetInstalledVoices(CultureInfo.CurrentCulture).FirstOrDefault();

                if (installedVoice != null)

                {

                    speechSynthesizer.SetOutputToWaveFile(@"C:\1.wav");

                    speechSynthesizer.Speak(speektext);

                    speechSynthesizer.SetOutputToDefaultAudioDevice();

                }

            }

            catch

            {

            }

        }

    }

}


using System;

namespace Fountain.WinConsole.TextToSpeech

{

    internal class Program

    {

        static void Main(string[] args)

        {

            TextToSpeech.SpeakingToFile(4, 100, "欢迎关注dotNet开发技术分享");

            Console.ReadKey();

        }

    }

}

问题

使用 System.Speech 需要操作系统的支持,对于一些阉割版Windows 7或 10 是没有这项功能的,这会使功能失效。可以通过重装完整的系统或者修复其语音播报的功能。

小结

以上只是使用 .NET 提供的库来实现文本转语言播报的内容,当然现在有很多第三方语音库,如Microsoft.CognitiveServices.Speech、讯飞、百度语音等。本文的内容希望能为感兴趣的伙伴提供参考,如有不到之处,请多多包涵。


该文章在 2024/6/28 11:20:34 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved