【学习笔记】WP7语音API MircoPhone

这篇文章属于学习笔记,拿给自己看的,只贴了代码,除了注释没有做过多的解释,还请通过“搜索娘”找到这篇罪孽文章的童鞋原谅快哭了

Microphone microphone = Microphone.Default;
        MemoryStream audioStream = new MemoryStream();
        SoundEffectInstance recordedSound;
        bool isRecording = false;//是否正在录音
        byte[] buffer;
        bool recordingStopped = false;
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            //模拟XNA的loop
            DispatcherTimer dt = new DispatcherTimer();
            dt.Interval = TimeSpan.FromMilliseconds(30);
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();
            SoundEffect.MasterVolume = 1.0f;
        }

      

        void dt_Tick(object sender, EventArgs e)
        {
            try
            {
                FrameworkDispatcher.Update();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }

            if (recordingStopped)
            {
                recordingStopped = false;
                Debug.WriteLine("准备播放声音");
                //如果已经停止录音 则播放声音
                Thread th = new Thread(new ThreadStart(() => {
                    //多声道播放语音
                    SoundEffect sound = new SoundEffect(audioStream.ToArray(), microphone.SampleRate, AudioChannels.Stereo);
                    Debug.WriteLine("声音流大小:"+audioStream.ToArray().Length.ToString());
                    recordedSound = sound.CreateInstance();

                    Dispatcher.BeginInvoke(() => {
                        recordedSound.Pitch = (float)pitchSlider.Value;
                        recordedSound.Pan = (float)panSlider.Value;
                        recordedSound.Volume = (float)volSlider.Value;
                    });
                    recordedSound.Play();
                }));

                th.IsBackground = true;
                th.Start();
                Debug.WriteLine("开始播放声音");
            }
        }

        private void recordButton_Click(object sender, RoutedEventArgs e)
        {
            if (!isRecording)
            {
                //开始录音
                if (Microphone.Default == null)
                {
                    return;
                }
                //缓冲时间为500毫秒
                microphone.BufferDuration = TimeSpan.FromMilliseconds(500);

                //500毫秒需要的缓冲区的大小
                buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];

                audioStream.SetLength(0);

                microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);

                microphone.Start();

                isRecording = true;//下次点击的时候  结束录音

                recordButton.Content = "完成录音录音";

            }
            else
            {
                //停止录音
                recordButton.Content = "开始录音";

                microphone.Stop();

                recordingStopped = true;

                isRecording = false;
             
            }
        }

        void microphone_BufferReady(object sender, EventArgs e)
        {
            microphone.GetData(buffer);
            //写入内存流中
            audioStream.Write(buffer, 0, buffer.Length);
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值