winform语音播报

第一步:

在Nut中引用:

winform语音播报_第1张图片

第二步:

创建一个窗体,在文本框中输入文字,点击按钮将文本框的内容语音播放出来

winform语音播报_第2张图片

点击按钮

   private void button1_Click(object sender, EventArgs e)
        {
            new Lisen().Tts(richTextBox1.Text);
        
        }
 public class Lisen
    {
        private readonly Asr _asrClient;
        private readonly Tts _ttsClient;
        //App ID: 7948081         
        //API Key: 1G4dOtYNSCud03x5CKM77grtCPTky2z4         
        //Secret Key: vluE1PUsviXuoXgNaADUKgfO7kl402YG        
        public Lisen()        {
            _asrClient = new Asr("7948081","1G4dOtYNSCud03x5CKM77grtCPTky2z4", "vluE1PUsviXuoXgNaADUKgfO7kl402YG");
            _ttsClient = new Tts("1G4dOtYNSCud03x5CKM77grtCPTky2z4", "vluE1PUsviXuoXgNaADUKgfO7kl402YG");
        }
        // 识别本地文件       
        //public void AsrData()       
        //{        
        //    var data = File.ReadAllBytes("语音pcm文件地址");
        //    var result = _asrClient.Recognize(data, "pcm", 16000);       
        //    Console.Write(result);        
        //}         
        // 识别URL中的语音文件       
        //public void AsrUrl()       
        //{        
        //    var result = _asrClient.Recoginze(    
        //       "http://xxx.com/待识别的pcm文件地址",      
        //       "http://xxx.com/识别结果回调地址",        
        //       "pcm",      
        //       16000);        
        //   Console.WriteLine(result);     
        //}        
        ///List listdata = new List();     
        //         
        // 语音文件的生成       
        //         
        /// 需要生成的语音字符        
       ///         
       public string Tts(string str)
        {
            lock (this)
            {
                string pathwave = @"D:\3.mp3";
                // 可选参数            
                var option = new Dictionary()
                {
                    { "spd", 5}, // 语速          
                    { "vol", 10}, // 音量            
                    { "per", 0}  // 发音人,4:情感度丫丫童声  
                };
                var result = _ttsClient.Synthesis(str, option);
                if (result.ErrorCode == 0)  // 或 result.Success 
                {                    
                    File.WriteAllBytes(pathwave, result.Data);
                    var mp3FileName = pathwave;
                    var outputFileName = mp3FileName.Substring(0, mp3FileName.Length - 3) + "wav";
                    Mp3ToWav(mp3FileName, outputFileName);//mp3转wav文件             
                    SoundPlayer player = new SoundPlayer();//进行语音文件播放                
                    player.SoundLocation = outputFileName;
                    player.Load(); //同步加载声音         
                    player.Play(); //启用新线程播放    
                }
                return "";
            }
        }
        /// MP3文件转WAV

        /// 

        /// MP3文件路径

        /// WAV文件输出目录
        public static string Mp3ToWav(string mp3File, string outputFile)
        {
            using (Mp3FileReader reader = new Mp3FileReader(mp3File))
            {
                var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit        
                using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)){
                    WaveFileWriter.CreateWaveFile(outputFile, conversionStream);
                }
                return outputFile;
            }
        }
        public static string Convert2Wav(string filePath)
        {
            string directoryName = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); string tempDir = directoryName + "\\temp" + DateTime.Now.ToString("yyyyMMddHHmmss") + "\\"; if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); }
            if (filePath.EndsWith(".wav", StringComparison.CurrentCultureIgnoreCase))
            {
                using (var reader = new WaveFileReader(filePath))
                {
                    var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit              
                    using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)){
                        WaveFileWriter.CreateWaveFile(tempDir + fileName, conversionStream);
                    }
                }
            }
            else if (filePath.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase)){
                using (Mp3FileReader reader = new Mp3FileReader(filePath)){
                    var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit    
                    using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)){
                        WaveFileWriter.CreateWaveFile(tempDir + fileName, conversionStream);
                    }
                }
            }
            return tempDir + fileName;
        }
    }             

 

 

 

 

你可能感兴趣的:(C#)