微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在WP7上录制音频

 

这篇文章展示了如何在Windows Phone 7上录制音频。
简介
通过windows phone设备的麦克风来录制音频是非常容易的。Microsoft.Xna.Framework.Audio.Microphone支持这项功能
    Start-从话筒中捕捉音频
    Stop-停止从话筒中捕捉音频
    GetData-将数据从话筒中转换到缓冲区中 
下面的示例代码假设在应用程序网页上有三个按钮- start,stop和 play。
参考和初始化
在Silverlight应用程序中使用话筒,我们提到了Microsoft.Xna.Framework,我们将它们加入到应用程序网页中来实施录制。
using System.IO;
System.Windows.Threading;
Microsoft.Xna.Framework;
Microsoft.Xna.Framework.Audio;

并声明下面这些变量:
Microphone microphone = Microphone.Default;
    byte[] buffer;
    MemoryStream stream = new MemoryStream();
SoundEffect sound;

开始录制
下面这些代码显示了如何使用话筒来录制音频:
private void recordButton_Click(object sender,RoutedEventArgs e)
{
microphone.BufferDuration = TimeSpan.FromMilliseconds(1000);
buffer = new [microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.Start();
}

停止录制
停止录制音频的代码
stopButton_Click({
if (microphone.State == Microphonestate.Started)
{
        microphone.Stop();
}
}

捕获缓存区中的记录
为了捕获缓存区中的记录,我们需要在页面中的构造函数添加事件处理程序。
microphone.BufferReady += EventHandler<EventArgs>(microphone_BufferReady);

并使用下面的event handler 代码
void  microphone_BufferReady(ottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,EventArgs e)
{
microphone.GetData(buffer);
stream.Write(buffer,buffer.Length);
}

播放录制的音频
在录制完成后进行播放
playButton_Click({
sound = SoundEffect(stream.ToArray(),microphone.SampleRate,AudioChannels.Mono);
sound.Play();
}

最后但并非不重要
为了使我们上述所介绍的代码能够运行,我们需要做更多的工作,当我们在Silverlight应用程序中使用XNA Game Studio,我们就需要模拟Game循环来使我们的程序能够正常的实施。定义下面的App.class XNAAsyncdispatcher类并将它添加到App()构造函数的下一行。
ApplicationLifetimeObjects.Add(XNAAsyncdispatcher(TimeSpan.FromMilliseconds(50)));
 
public class XNAAsyncdispatcher : Iapplicationservice
{
private dispatcherTimer _frameworkdispatcherTimer;
public XNAAsyncdispatcher(System.TimeSpan dispatchInterval)
{
        Frameworkdispatcher.Update();
        this._frameworkdispatcherTimer = dispatcherTimer();
._frameworkdispatcherTimer.Tick += newEventHandler(frameworkdispatcherTimer_Tick);
._frameworkdispatcherTimer.Interval = dispatchInterval;
}
Iapplicationservice.StartService(applicationserviceContext context)
{
._frameworkdispatcherTimer.Start();
}
  
Iapplicationservice.StopService()
{
._frameworkdispatcherTimer.Stop();
}
  
frameworkdispatcherTimer_Tick(ottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,EventArgs e)
{
Frameworkdispatcher.Update();
   }

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐