博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Pocket PC 录音(转)

Posted on 2008-07-31 00:38  雨过流痕  阅读(308)  评论(0编辑  收藏  举报

#region 作者:ah, 2007-10-31
  /**//*
   * 版本 1.0
   * 维护者:
   * 最近维护日期:
   *
   */
  #endregion
  using System;
 using System.Drawing;
 using System.Collections;
 using System.Windows.Forms;
 using System.Data;
 using OpenNETCF.Multimedia.Audio;
 using System.IO;
 using System.Reflection;
 
 namespace ppcvoicerecorder
 {
     public partial class frmRecorder : Form   
     {         
         private Player player;
         private Recorder recorder;
         private Stream stream;
         private int timeLeft;
     
         private const int RECORD_LENGTH = 600;
         private const int MAX_RECORD_LENGTH = 600;//录音最大长度
         private  string TEMP_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
         private string TEMP_FILE = SystemInfoFilePath();
         private DateTime STARTRECORD_TIME;//开始录音时间
         private DateTime ENDRECORE_TIME;//结实录音时间
         ConnDate conn = new ConnDate();
 
         public frmRecorder()
         {
             InitializeComponent();
 
             lblTime.Width = this.Width;         
             // 实例化录音机
             recorder = new Recorder();
             recorder.DoneRecording += new WaveFinishedHandler(recorder_DoneRecording);
             // 实例化播放器
             player = new Player();
             player.DonePlaying += new WaveDoneHandler(player_DonePlaying);          
         }
       
 
         void recorder_DoneRecording()
         {
             mniRecord.Text = "录音";
             tmrRecord.Enabled = false;
           
         }
 
         设置本地存储路径#region 设置本地存储路径
 
         /**//// <summary>
         /// 获取系统配置文件的ppc本地存储路径
         /// </summary>
         private static string SystemInfoFilePath()
         {         
                 string strDbfile = string.Format("{0}\\"+ConnDate.CodeGenerator("I")+".wav",
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
                 return string.Format("{0}", strDbfile);          
         }
         #endregion
 
         private DialogResult _isCancel = DialogResult.No;
 
         public DialogResult IsCancel
         {
             get { return _isCancel; }
         }      
 
         void player_DonePlaying(object sender, IntPtr wParam, IntPtr lParam)
         {
            // mniPlay.Text = "播放";
         }
 
         private void tmrRecord_Tick(object sender, EventArgs e)
         {
             timeLeft--;
             if (timeLeft >0)
             {
                 lblTime.Text = string.Format("0:{0:00}", MAX_RECORD_LENGTH - timeLeft);
             }
         }
       
         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 == "停止/确认")
                {
                    tmrRecord.Enabled = false;                  
                    // 停止录音
                    this.ENDRECORE_TIME = DateTime.Now;
                    this.lblTime.Text = conn.DateDiff(this.STARTRECORD_TIME,this.ENDRECORE_TIME);
                    recorder.Stop();
                    _isCancel = DialogResult.OK;
                    mniRecord.Text = "录音";                 
                    this.Close();                  
                }
                else
                {
                    this.STARTRECORD_TIME = DateTime.Now;
                    stream = PrepareTempFile();                
                    timeLeft = RECORD_LENGTH;
                    tmrRecord.Enabled = true;

                    // 开始录音
                    recorder.RecordFor(stream, RECORD_LENGTH, SoundFormats.Mono8bit11kHz);
                    mniRecord.Text = "停止/确认";                  
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /**//// <summary>
        /// 取得录音文件路径
        /// </summary>
        /// <returns>返回路径</returns>
        public string GetFileInfo()
        {
            try
            {
                return this.TEMP_FILE;
            }
            catch (Exception err)
            {
                return string.Empty;
                MessageBox.Show(err.Message);
            }
        }       

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

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