⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 wince 下的录音软件,c#编写.使用OpenNetCF组件完成录音功能.
💻 CS
字号:
#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using OpenNETCF.Multimedia.Audio;
using System.IO;

#endregion

namespace VoiceRecorder
{
    /// <summary>
    /// 窗体的摘要描述。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private HotKeys hotkeys;
        private Player player;
        private Recorder recorder;
        private Stream stream;
        private int timeLeft;

        private const int RECORD_LENGTH = 20;
        private const string TEMP_PATH = @"\Temp";
        private const string TEMP_FILE = @"\Temp\ipspsd_record.wav";

        private MenuItem mniRecord;
        private MenuItem mniMenu;
        private MenuItem mniPlay;
        private MenuItem mniAbout;
        private MenuItem mniExit;
        private Timer tmrRecord;
        private Label lblTime;
        /// <summary>
        /// 窗体的主菜单。
        /// </summary>
        private System.Windows.Forms.MainMenu mainMenu1;

        public Form1()
        {
            InitializeComponent();

            lblTime.Width = this.Width;

            // 实例化录音机
            recorder = new Recorder();
            recorder.DoneRecording += new WaveFinishedHandler(recorder_DoneRecording);

            // 实例化播放器
            player = new Player();
            player.DonePlaying += new WaveDoneHandler(player_DonePlaying);

            hotkeys = new HotKeys();
            hotkeys.Register(Keys.D5, HotKeys.KeyModifiers.None);
            hotkeys.KeyPressed += new HotKeys.KeyPressedEventHandler(hotkeys_KeyPressed);
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            hotkeys.UnRegister(Keys.D5);
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.mniRecord = new System.Windows.Forms.MenuItem();
            this.mniMenu = new System.Windows.Forms.MenuItem();
            this.mniPlay = new System.Windows.Forms.MenuItem();
            this.mniAbout = new System.Windows.Forms.MenuItem();
            this.mniExit = new System.Windows.Forms.MenuItem();
            this.tmrRecord = new System.Windows.Forms.Timer();
            this.lblTime = new System.Windows.Forms.Label();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.mniRecord);
            this.mainMenu1.MenuItems.Add(this.mniMenu);
            // 
            // mniRecord
            // 
            this.mniRecord.Text = "Record";
            this.mniRecord.Click += new System.EventHandler(this.mniRecord_Click);
            // 
            // mniMenu
            // 
            this.mniMenu.MenuItems.Add(this.mniPlay);
            this.mniMenu.MenuItems.Add(this.mniAbout);
            this.mniMenu.MenuItems.Add(this.mniExit);
            this.mniMenu.Text = "Menu";
            // 
            // mniPlay
            // 
            this.mniPlay.Text = "Play";
            this.mniPlay.Click += new System.EventHandler(this.mniPlay_Click);
            // 
            // mniAbout
            // 
            this.mniAbout.Text = "About";
            this.mniAbout.Click += new System.EventHandler(this.mniAbout_Click);
            // 
            // mniExit
            // 
            this.mniExit.Text = "Exit";
            this.mniExit.Click += new System.EventHandler(this.mniExit_Click);
            // 
            // tmrRecord
            // 
            this.tmrRecord.Interval = 1000;
            this.tmrRecord.Tick += new System.EventHandler(this.tmrRecord_Tick);
            // 
            // lblTime
            // 
            this.lblTime.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Bold);
            this.lblTime.Location = new System.Drawing.Point(3, 65);
            this.lblTime.Size = new System.Drawing.Size(173, 57);
            this.lblTime.Text = "Press 5 to start recording.";
            this.lblTime.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(176, 180);
            this.Controls.Add(this.lblTime);
            this.Menu = this.mainMenu1;
            this.Text = "Voice Recorder";

        }

        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            Application.Run(new Form1());
        }

        void recorder_DoneRecording()
        {
            mniRecord.Text = "Record";
            tmrRecord.Enabled = false;
            mniPlay.Enabled = true;
        }

        void player_DonePlaying(object sender, IntPtr wParam, IntPtr lParam)
        {
            mniPlay.Text = "Play";
        }

        private void tmrRecord_Tick(object sender, EventArgs e)
        {
            timeLeft--;
            if (timeLeft > 0)
            {
                lblTime.Text = string.Format("0:{0:00}", timeLeft);
            }
        }

        private void hotkeys_KeyPressed(Keys key)
        {
            if (key == Keys.D5)
            {
                Record();
            }
        }

        private Stream PrepareTempFile()
        {
            // 检查临时目录是否存在
            if (!Directory.Exists(TEMP_PATH))
            {
                Directory.CreateDirectory(TEMP_PATH);
            }

            // 检查临时录音文件是否存在
            if (File.Exists(TEMP_FILE))
            {
                File.Delete(TEMP_FILE);
            }

            stream = File.OpenWrite(TEMP_FILE);

            return stream;
        }

        private void Record()
        {
            try
            {
                if (mniRecord.Text == "Stop")
                {
                    tmrRecord.Enabled = false;
                    lblTime.Text = "Press 5 to start recording.";

                    // 停止录音
                    recorder.Stop();

                    mniRecord.Text = "Record";
                    mniPlay.Enabled = true;
                }
                else
                {
                    stream = PrepareTempFile();

                    lblTime.Text = string.Format("0:{0:00}", RECORD_LENGTH);
                    timeLeft = RECORD_LENGTH;
                    tmrRecord.Enabled = true;

                    // 开始录音
                    recorder.RecordFor(stream, RECORD_LENGTH, SoundFormats.Mono8bit11kHz);

                    mniRecord.Text = "Stop";
                    mniPlay.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Play()
        {
            if (mniPlay.Text == "Stop")
            {
                player.Stop();
                mniPlay.Text = "Play";
            }
            else
            {
                Stream s = File.OpenRead(TEMP_FILE);

                // 播放录音
                player.Play(s);
                //player.Volume = 0x7fffffff;

                mniPlay.Text = "Stop";
            }
        }

        private void mniRecord_Click(object sender, EventArgs e)
        {
            Record();
        }

        private void mniPlay_Click(object sender, EventArgs e)
        {
            Play();
        }

        private void mniAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Copyright (c) 2007 Bob Li, All Rights Reserved", "About Voice Recorder");
        }

        private void mniExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -