Пример #1
0
        /// <summary>
        /// 语音播放
        /// </summary>
        public static void NewRead()
        {
            try
            {
                //读取已有文件
                string            wavPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Sounds\\ring.wav";//要读的音频文件地址
                SpVoiceClass      pp      = new SpVoiceClass();
                SpFileStreamClass spFs    = new SpFileStreamClass();
                spFs.Open(wavPath, SpeechStreamFileMode.SSFMOpenForRead, true);
                ISpeechBaseStream Istream = spFs as ISpeechBaseStream;

                //文字转语音播放
                SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
                SpVoice spVoice = new SpVoice();                                                                                  //声源
                spVoice.Rate   = Convert.ToInt32(System.Configuration.ConfigurationManager.ConnectionStrings["Rate"].ToString()); //速度
                spVoice.Volume = 100;
                spVoice.WaitUntilDone(-1);
                spVoice.SpeakStream(Istream, spFlags);
                //循环播放
                for (int i = 0; i < readCount; i++)
                {
                    spVoice.WaitUntilDone(-1);
                    spVoice.Speak(readTxt, spFlags);//文字转语音播放
                }
                spFs.Close();
                //直接读取音频文件
                //SoundPlayer soundPlayer = new SoundPlayer();
                //soundPlayer.SoundLocation = wavPath;
                //soundPlayer.Load();
                //soundPlayer.Play();
            }
            catch (Exception err)
            {
                MessageBox.Show("语音播报失败!");
                CommonalityEntity.WriteTextLog(err.ToString());
            }
        }
Пример #2
0
        public static void NewRead(string content)
        {
            string       wavPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ring.wav";//要读的音频文件地址
            SpVoiceClass pp      = new SpeechLib.SpVoiceClass();

            SpeechLib.SpFileStreamClass spFs = new SpFileStreamClass();
            spFs.Open(wavPath, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
            SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
            //SoundPlayer soundPlayer = new SoundPlayer();
            //soundPlayer.SoundLocation = wavPath;
            //soundPlayer.Load();
            //soundPlayer.Play();
            SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpVoice spVoice = new SpVoice(); //声源

            spVoice.Rate   = -5;             //速度
            spVoice.Volume = 100;
            //播放
            spVoice.SpeakStream(Istream, spFlags);
            spVoice.WaitUntilDone(-1);
            spVoice.Speak(content, spFlags);
            spFs.Close();
            //生成文件
            //try
            //{
            //    SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
            //    SpFileStream SpFileStream = new SpFileStream();
            //    SpFileStream.Open(@"C:\Users\EMEWE\Desktop\Test.wav", SpFileMode, false);
            //    spVoice.AudioOutputStream = SpFileStream;//设定voice的输出为Stream
            //    spVoice.Speak(txtContent.Text.Trim(), spFlags);
            //    spVoice.WaitUntilDone(Timeout.Infinite);//Using System.Threading;
            //    SpFileStream.Close();
            //    MessageBox.Show("生成成功!");
            //}
            //catch { MessageBox.Show("生成失败!"); }
        }