使用windows 窗体应用程序编写简单的阅读器

来源:互联网 发布:黄金交易软件手机版 编辑:程序博客网 时间:2024/05/22 16:42


using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using SpeechLib;//添加语音引用


namespace Speech
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnRead_Click(object sender, EventArgs e)
        {
            Speak(txtContent.Text);
        }
        
        private void Speak(string speak)
        {  
            SpVoice voice = new SpVoice();
            voice.Rate = -4; 
            voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0);  
            voice.Speak(speak, SpeechVoiceSpeakFlags.SVSFDefault);  
        }


        //保存语音 
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
                SpVoice Voice = new SpVoice();
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
                sfd.Title = "Save to a wave file";
                sfd.FilterIndex = 2;
                sfd.RestoreDirectory = true;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
                    SpFileStream SpFileStream = new SpFileStream();
                    SpFileStream.Open(sfd.FileName, SpFileMode, false);
                    Voice.AudioOutputStream = SpFileStream;
                    Voice.Speak(this.txtContent.Text, SpFlags);
                    Voice.WaitUntilDone(100);
                    SpFileStream.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
0 0