C#调用TTS语音

运行界面如下图

代码如下

卡界面运行


        private void button2_Click(object sender, EventArgs e)
        {
            //https://developer.aliyun.com/article/264926#:~:text=%E5%8E%9F%E6%96%87%3A%20C%23%E8%AF%AD%E9%9F%B3%E6%9C%97%E8%AF%BB%E6%96%87%E6%9C%AC%20%E2%80%94%20TTS%E7%9A%84%E5%AE%9E%E7%8E%B0,TTS%2C%20Text%20To%20Speech%E7%9A%84%E7%BC%A9%E5%86%99%EF%BC%8C%E6%98%AF%E4%BD%BF%E7%94%A8%E8%AF%AD%E9%9F%B3%E6%9C%97%E8%AF%BB%E6%96%87%E6%9C%AC%E7%9A%84%E6%8A%80%E6%9C%AF%E3%80%82
            Type type = Type.GetTypeFromProgID("SAPI.SpVoice");

            dynamic spVoice = Activator.CreateInstance(type);

            spVoice.Speak("你好,欢迎使用 CSharp 4.0!");
            spVoice.Speak("你好,请打开资源管理器");
            spVoice.Speak("查看设备管理器内端口");

        }

 后台运行


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Type type = Type.GetTypeFromProgID("SAPI.SpVoice");

            dynamic spVoice = Activator.CreateInstance(type);

            spVoice.Speak("后台运行声音测试程序");
            spVoice.Speak(" 从新开始 ");
            spVoice.Speak("我们都是测试程序");

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //this.backgroundWorker1.CancelAsync();
            ///if (!backgroundWorker1.WorkerReportsProgress)
            if(!this.backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();

        }

需要添加 控件(当然也可以代码添加)

 

参考链接

C#语音朗读文本 — TTS的实现-阿里云开发者社区 (aliyun.com)

主要是改造了后台运行。

特此记录

anlog

2022年12月4日

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Microsoft提供的Speech API来实现这个功能。Speech API是一个Windows API,可用于将文本转换为语音或将语音转换为文本。Edge浏览器提供了对Speech API的支持,可以通过C#代码调用Speech API以将文本转换为语音。 下面是一个简单的示例代码,使用SpeechSynthesizer类将文本转换为语音并播放出来: ```csharp using System.Speech.Synthesis; SpeechSynthesizer synthesizer = new SpeechSynthesizer(); synthesizer.SetOutputToDefaultAudioDevice(); synthesizer.Speak("Hello World"); ``` 在上面的示例中,我们创建了一个SpeechSynthesizer对象,并使用SetOutputToDefaultAudioDevice方法将输出配置为默认音频设备。然后,我们调用Speak方法将文本转换为语音并播放出来。 如果您想要使用Edge浏览器进行语音合成,请按照以下步骤操作: 1. 确保您的Windows版本支持Speech API和Edge浏览器。 2. 在Visual Studio中创建一个新的C#控制台应用程序。 3. 在程序包管理器控制台中安装Microsoft.Speech.SDK库。 4. 将以下代码添加到您的程序中: ```csharp using System.Speech.Synthesis; SpeechSynthesizer synthesizer = new SpeechSynthesizer(); synthesizer.SetOutputToAudioStream( new EdgeBrowserAudioStream(), new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null)); synthesizer.Speak("Hello World"); public class EdgeBrowserAudioStream : System.IO.Stream { private readonly System.IO.Stream _underlyingStream; public EdgeBrowserAudioStream() { _underlyingStream = Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlProcess.GetWebViewCompatibleStream(new System.IO.MemoryStream()); } public override bool CanRead => _underlyingStream.CanRead; public override bool CanSeek => _underlyingStream.CanSeek; public override bool CanWrite => _underlyingStream.CanWrite; public override long Length => _underlyingStream.Length; public override long Position { get => _underlyingStream.Position; set => _underlyingStream.Position = value; } public override void Flush() => _underlyingStream.Flush(); public override int Read(byte[] buffer, int offset, int count) => _underlyingStream.Read(buffer, offset, count); public override long Seek(long offset, SeekOrigin origin) => _underlyingStream.Seek(offset, origin); public override void SetLength(long value) => _underlyingStream.SetLength(value); public override void Write(byte[] buffer, int offset, int count) => _underlyingStream.Write(buffer, offset, count); } ``` 在上面的代码中,我们创建了一个SpeechSynthesizer对象,并使用SetOutputToAudioStream方法将输出配置为Edge浏览器的音频流。我们还创建了一个EdgeBrowserAudioStream类,该类实现了System.IO.Stream接口,并将其传递给SetOutputToAudioStream方法。EdgeBrowserAudioStream类是必需的,因为SpeechSynthesizer类只能将音频输出到System.IO.Stream对象。最后,我们调用Speak方法将文本转换为语音。 请注意,这里使用的是Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlProcess.GetWebViewCompatibleStream方法来获取Edge浏览器的音频流。这是由于Speech API需要使用一个特殊的音频流,才能与Edge浏览器兼容。 希望这个示例对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值