private void SaveWavButton_Click(object sender, System.EventArgs e) { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "All Files (*.*)|*.*|Wave Files (*.wav)|*.wav"; saveFile.FilterIndex = 2; saveFile.RestoreDirectory = true; if (saveFile.ShowDialog() == DialogResult.OK) { AddEventMessage("INFO: Saving to a wave file..."); // We need a file stream. SpFileStreamClass spFile = new SpFileStreamClass(); // Try and use the user specified format for the // wave file. if (FormatCombo.SelectedIndex != -1) { try { SpeechAudioFormatType newFormat = (SpeechAudioFormatType) Enum.Parse(typeof(SpeechAudioFormatType), (string)FormatCombo.SelectedItem); spFile.Format.Type = newFormat; } catch (Exception) { AddEventMessage("ERROR: Error while attempting to set audio format."); } } spFile.Open(saveFile.FileName, SpeechStreamFileMode.SSFMCreateForWrite, false); // We don't want SAPI to change the format, because // we might have done so already. Voice.AllowAudioOutputFormatChangesOnNextSet = false; Voice.AudioOutputStream = spFile; this.Speak(); Voice.WaitUntilDone(-1); spFile.Close(); AddEventMessage("INFO: Wave file written successfully."); } }
/// <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()); } }
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("生成失败!"); } }