Monday 10 May 2010

WP7 and Microsoft.Xna.Framework.Audio.Microphone

I was looking to try out the Microphone functionality as detailed by Peter Foot in the following blog post :-
http://mobileworld.appamundi.com/blogs/peterfoot/archive/2010/03/26/audio-recording-in-wp7.aspx

This is the code that I came up with :-

public partial class MainPage : PhoneApplicationPage
{
private Microphone currentMic = Microphone.Default;
private byte[] currentData;
private MemoryStream currentStream = new MemoryStream();

public MainPage()
{
InitializeComponent();

SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
}

private void m_BufferReady(object sender, EventArgs e)
{
currentMic.GetData(currentData);
currentStream.Write(currentData, 0, currentData.Length);
}

private void Start_Click(object sender, RoutedEventArgs e)
{
currentMic.BufferDuration = TimeSpan.FromMilliseconds(1000);
currentData = new byte[currentMic.GetSampleSizeInBytes(currentMic.BufferDuration)];
currentMic.BufferReady += new EventHandler(m_BufferReady);

currentMic.Start();
}

private void Stop_Click(object sender, RoutedEventArgs e)
{
currentMic.Stop();
currentStream.Close();
}

private void Play_Click(object sender, RoutedEventArgs e)
{
SoundEffect currentSoundEffect = new SoundEffect(currentStream.ToArray(), currentMic.SampleRate, AudioChannels.Mono);
currentSoundEffect.Play();
}
}

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Excellent, thanks

    ReplyDelete
  3. Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me
    yeti usb microphone

    ReplyDelete