Compartilhar via


How to access the microphone for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

To capture input from the Windows Phone microphone, use the Microphone class from the XNA Framework.

To add microphone support to a Windows Phone app

  1. Create a new project by selecting the File | New Project menu command.

  2. The New Project window will be displayed. Expand the Visual C# templates, and then select the Windows Phone templates.

  3. Select the **Windows Phone App ** template. Fill in the project name as desired.

  4. In the Solution Explorer, right-click References and choose Add Reference...

  5. Choose Microsoft.Xna.Framework from the list of .NET components and click the OK button.

  6. If you see a dialog box that warns about adding a reference to a Windows Phone assembly, click the Yes button.

  7. Add the following using statements to the top of your MainPage.xaml.cs file:

    using System.IO;
    using System.Windows.Threading;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    
  8. In MainPage.xaml.cs, declare variables as global members of your MainPage class:

    public partial class MainPage : PhoneApplicationPage
    {
        Microphone microphone = Microphone.Default;
        byte[] buffer;
        MemoryStream stream = new MemoryStream();
        SoundEffect sound;     
    // ...
    
  9. Since we are using XNA Game Studio in a Windows Phone app, we need to simulate the Game loop that XNA Game Studio normally implements for us. Add the following code to the constructor of your MainPage class after the call to InitializeComponent to simulate the XNA Game Studio game loop:

    // 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();
    
  10. Add a new Microphone.BufferReady event handler to the constructor of your MainPage class:

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

    Your declaration of variables and finished constructor should look like this:

    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);
        }
         ...
    
  11. Implement the BufferReady event handler. This handler copies the data from the microphone into a buffer and writes that buffer to a stream.

    void microphone_BufferReady(object sender, EventArgs e)
    {
        microphone.GetData(buffer);
        stream.Write(buffer, 0, buffer.Length);
    }
    
  12. Add a way for the user to start capturing audio from the microphone. This event handler for a record button Click event allocates a buffer large enough to hold 1 second of audio and begins collecting that data by calling Microphone.Start.

    private void recordButton_Click(object sender, RoutedEventArgs e)
    {
        microphone.BufferDuration = TimeSpan.FromMilliseconds(1000);
        buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
        microphone.Start();
    }
    
  13. Add a way for the user to stop capturing audio from the microphone. This event handler for a stop button Click event checks to see whether the microphone is currently collecting input. If it is, the code calls Microphone.Stop to end the recording.

    private void stopButton_Click(object sender, RoutedEventArgs e)
    {
        if (microphone.State == MicrophoneState.Started)
        {
            microphone.Stop();
        }
    }
    
  14. Add a way for the user to play back the captured audio. This event handler for a play button Click event allocates a new SoundEffect object using the stream where the audio data was saved. Then, it calls the SoundEffect.Play method.

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

These are the basic steps necessary to collect audio data from the microphone in a Windows Phone app by using the Microphone class from the XNA Framework. For a more thorough example, including how to monitor for when the SoundEffect stops playing, see the Microphone Sample.