您的位置:首页 > 移动开发 > Objective-C

WP7应用程序中添加麦克风支持

2012-04-15 21:51 190 查看



通过选择“文件 | 新建项目”菜单命令来创建一个新项目。

将显示“新建项目”窗口。展开“Visual C#”模板,然后选择“Silverlight for Windows Phone”模板。

选择“Windows Phone 应用程序”模板。填写所需的项目名称。

在“解决方案资源管理器”中,右键单击“引用”,然后选择“添加引用...”。

从 .NET 组件列表中选择 Microsoft.Xna.Framework,然后单击“确定”按钮。

如果您看到一个对话框,该对话框警告有关向 Silverlight 程序集添加引用的信息,请单击“是”。

向 MainPage.xaml.cs 文件的顶部添加以下 using 语句:

using System.IO;
using System.Windows.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;


在 MainPage.xaml.cs 中,将变量声明为 MainPage 类的全局成员:

public partial class MainPage : PhoneApplicationPage
{
Microphone microphone = Microphone.Default;
byte[] buffer;
MemoryStream stream = new MemoryStream();
SoundEffect sound;
// ...


由于我们在 Silverlight 应用程序中使用 XNA Game Studio,因此需要模拟 XNA Game Studio 在正常情况下为我们实现的游戏循环。将以下代码添加到 MainPage 类构造函数中对 InitializeComponent 的调用之后,以模拟
XNA Game Studio 游戏循环:

// Timer to simulate the XNA Game Studio game loop (Microphone is from XNA Game Studio)
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(50);
dt.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
dt.Start();


向 MainPage 类中添加一个新的 Microphone.BufferReady 事件处理程序:

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


变量声明以及完成的构造函数应如下所示:

public partial class MainPage : PhoneApplicationPage
{
Microphone microphone = Microphone.Default;
byte[] buffer;
MemoryStream stream = new MemoryStream();
SoundEffect sound;

// Constructor
public MainPage()
{
InitializeComponent();

// Timer to simulate the XNA Game Studio game loop (Microphone is from XNA Game Studio)
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
dt.Start();

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



实现 BufferReady 事件处理程序。该处理程序将麦克风的数据复制到缓冲区中并将该缓冲区写入一个流。

void  microphone_BufferReady(object sender, EventArgs e)
{
microphone.GetData(buffer);
stream.Write(buffer, 0, buffer.Length);
}


添加用户开始从麦克风捕获音频的方法。录制按钮 Click 事件的该事件处理程序分配一个足够大的缓冲区,可包含 1 秒钟的音频,并通过调用 Microphone.Start 开始收集该数据。

private void recordButton_Click(object sender, RoutedEventArgs e)
{
microphone.BufferDuration = TimeSpan.FromMilliseconds(1000);
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.Start();
}


添加用户停止从麦克风捕获音频的方法。停止按钮 Click 事件的该事件处理程序检查麦克风当前是否正在收集输入。如果是,则代码会调用 Microphone.Stop 结束录制。

private void stopButton_Click(object sender, RoutedEventArgs e)
{
if (microphone.State == MicrophoneState.Started)
{
microphone.Stop();
}
}


添加用户播放捕获的音频的方法。播放按钮 Click 事件的该事件处理程序使用其中保存音频数据的流分配一个新的 SoundEffect 对象。然后,该对象调用 SoundEffect.Play 方法。

private void playButton_Click(object sender, RoutedEventArgs e)
{
sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
sound.Play();
}


     15、存储音频

        using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())

            {

                using (IsolatedStorageFileStream stream = storage.CreateFile(memoInfo.FileName))

                {

                    // Write buffers from collection

                    foreach (byte[] buffer in memoBufferCollection)

                        stream.Write(buffer, 0, buffer.Length);

                    // Write partial buffer

                    stream.Write(extraBuffer, 0, extraBytes);

                }

但是在多个页面page中都要用到microphone时,会出现第二个页面录音不成功的现象

我遇到的情况是录到的stream总为空;

语音室microphon是共享的,第一个页面中

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


会对第二个页面产生影响,所有从第一个页面中将microphone.Stop()时要解除该绑定事件

microphone.BufferReady -= new EventHandler<EventArgs>(microphone_BufferReady);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息