• C#实现文本生成音频文件并播放


    本文将使用C#实现一个简单的winfrom窗体应用程序,可以自定义文本转换为声音进行播放,生成音频文件。采用Microsoft提供的SpeechSynthesizer类来实现这个功能。

    首先需要在代码中引用System.Speech.Synthesis命名空间,就可以使用SpeechSynthesizer类。

    using System.Speech.Synthesis;
    
    • 1

    一、播放文本

    public void play(string text)
    {
        try
        {
            System.Threading.Thread.Sleep(500);
            WriteLogs("文本:" + text);
            if (text == "")
            {
                MessageBox.Show("请输入音频文本");
                return;
            }
            SpeechSynthesizer synth = new SpeechSynthesizer();
            PromptBuilder pb = new PromptBuilder();
            pb.AppendText(text);
            synth.Speak(pb);
            MessageBox.Show("播放成功");
        }
        catch (Exception e)
        {
            WriteLogs("播放失败:" + e.Message);
            MessageBox.Show("播放失败:" + e.Message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    二、播放路径

    public void playFile(string filename)
    {
        try
        {
            System.Threading.Thread.Sleep(500);
            if (filename == "")
            {
                MessageBox.Show("请输入音频路径");
                return;
            }
            SoundPlayer player = new SoundPlayer();
            player.SoundLocation = filename;
            player.Load();//加载
            player.Play();//播放
            MessageBox.Show("播放成功");
        }
        catch (Exception e)
        {
            MessageBox.Show("播放失败:" + e.Message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    三、生成音频

    public void save(string text, string filename)
    {
        try
        {
            System.Threading.Thread.Sleep(500);
            if (text == "")
            {
                MessageBox.Show("请输入音频文本");
                return;
            }
            else if (filename == "")
            {
                MessageBox.Show("请输入音频路径");
                return;
            }
            string file = filename.Substring(0, filename.LastIndexOf('/'));
            bool flag= !Directory.Exists(file);
            if (flag)
            {
                Directory.CreateDirectory(file);
            }
            SpeechSynthesizer synth = new SpeechSynthesizer();
            PromptBuilder pb = new PromptBuilder();
            pb.AppendText(text);
            synth.GetInstalledVoices().ToList().ForEach(voice => Console.WriteLine(voice.VoiceInfo.Name));
            synth.SelectVoice("Microsoft Huihui Desktop");
            // 设置合成音频的格式
            synth.SetOutputToWaveFile(filename, new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
            synth.Speak(pb);
            MessageBox.Show("生成成功");
        }
        catch (Exception e)
        {
            MessageBox.Show("生成失败:" + e.Message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    百度网盘下载程序源码。

    程序源码下载链接
    提取码:pakw
    在这里插入图片描述

  • 相关阅读:
    Windows基本命令学习
    Flutter快学快用08 单元测试:Flutter 应用单元测试,提升代码质量
    随想录一刷Day52——动态规划
    剑指 Offer 10- I. 斐波那契数列
    数据我爬定了,限流也挡不住,我说的
    Oracle函数如何返回多行多列?
    C# GDI 绘制饼图
    WiFi基础学习到实战(一)
    python基础(五)输入和while
    《计算机视觉》:图像滤波与边缘检测
  • 原文地址:https://blog.csdn.net/weixin_44547599/article/details/133904856