C#文字转语音功能

先上代码 真实可用:

注意:先对工程添加System.Speech,添加的方法是 [项目]------[添加引用]选中System.Speech 即可,如下图所示:

C#文字转语音功能

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

 

//语音功能引用------------------[begin]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//语音功能引用------------------[end]

namespace speek_text
{
    //主类----------[begin]
    class Program
    {

        //主函数------[begin]
        static void Main(string[] args)
        {
         


            //-----------------------------------读出文件内容---------------------------------
            SpeechSynthesizer voice = new SpeechSynthesizer();   //创建语音实例
            voice.Rate = -1; //设置语速,[-10,10]
            voice.Volume = 100; //设置音量,[0,100]
            //voice.SpeakAsync("Hellow Word");  //播放指定的字符串,这是异步朗读

            //voice.SelectVoice("Microsoft Lili");
            //下面的代码为一些SpeechSynthesizer的属性,看实际情况是否需要使用


            
            

            Console.WriteLine("[begin]--------------------------");
            string content = "跟着微软走妥妥的,C#文字转语音有很多参数,要好的效果得自己琢磨;先添加引用System.Speech程序集";
            //voice.SpeakAsyncCancelAll();  //取消朗读
            voice.Speak(content);  //同步朗读
            //voice.Pause();  //暂停朗读
            //voice.Resume(); //继续朗读
            voice.Dispose();  //释放所有语音资源
            Console.WriteLine("[end]--------------------------");


        }
        //主函数------[end]


    }
    //主类----------[end]
}