天天看點

錄音和朗誦的實作

錄音和朗誦的實作

如何實作語音和朗誦的功能:

using System;

using System.Windows.Forms;

using System.IO;

using System.Media;

using System.Runtime.InteropServices;

namespace 播放TTS

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        //根目錄

        string address = System.AppDomain.CurrentDomain.BaseDirectory;

        private void btnPlay_Click(object sender, EventArgs e)

            foreach (string s in textBox1.Text.Split(' '))

            {

                //string wavFile = @".\Video" + s + ".wav";

                if (File.Exists(wavFile))

                {

                    SoundPlayer sp = new SoundPlayer(wavFile);

                    sp.PlaySync();

                }

            }

        [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]

        private static extern int mciSendString(

         string lpstrCommand,

         string lpstrReturnString,

         int uReturnLength,

         int hwndCallback

        );

        private static void mciSendString(String cmd)

            mciSendString(cmd, "", 0, 0);

        private static void StartRecord()

            mciSendString("close movie");

            mciSendString("open new type WAVEAudio alias movie");

            mciSendString("record movie");

        private static void StopRecord(string filename)

            mciSendString("stop movie");

            mciSendString("save movie " + filename);

        //開始錄音

        private void btnBegin_Click(object sender, EventArgs e)

            StartRecord();

        private void btnStop_Click(object sender, EventArgs e)

            string title = ttbTitle.Text;

            string saveAddress = address + @"\Video\" + title.ToLower() + ".wav";

            if (File.Exists(saveAddress))

                MessageBox.Show("檔案已存在,重命名");

            else

                StopRecord(saveAddress);

                MessageBox.Show("錄音完畢");

    }

}

繼續閱讀