Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Handling Input on Windows Phone 7 : Microphone Input

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
10/23/2012 5:03:32 PM
The XNA Framework libraries make the Microphone available programmatically to applications. Add a reference to Microsoft.Xna.Framework assembly and a using clause for the Microsoft.Xna.Framework.Audio. The class of interest is the Microphone class that provides access to available microphones on the device.

The audio produced by the Microphone is 16-bit raw PCM. The audio can be played using the SoundEffect object without issue. To play recorded audio in a MediaElement control, the raw audio needs to be put into a .wmv file format.

For the sample project named MicrophoneWithSilverlight in the Ch03_HandlingInput_Part2 solution file the code uses the SoundEffect object to playback the audio.


App.xaml.cs is modified to include the XNAAsyncDispatcher class and add an instance to this.ApplicationLifetimeObjects. With the boiler code in place, the application builds out a simple UI to record, stop, and play microphone audio. A slider is configured as a pitch selector so that you make your voice sound like Darth Vader or a Chipmunk. Figure 1 shows the UI.

Figure 1. Microphone withSilverlight

Listing 1 shows the source code.

Example 1. MainPage.xaml.cs Code File
using System;
using System.IO;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Audio;

namespace MicrophoneWithSilverlight
{
  public partial class MainPage : PhoneApplicationPage
  {
    Microphone microphone = Microphone.Default;
    MemoryStream audioStream;

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

      microphone.BufferReady +=
        new EventHandler<EventArgs>(microphone_BufferReady);
      SoundEffect.MasterVolume = 1.0f;

      MicrophoneStatus.Text = microphone.State.ToString();
    }

    void microphone_BufferReady(object sender, EventArgs e)
    {
      byte[] audioBuffer = new byte[1024];
      int bytesRead = 0;

      while ((bytesRead = microphone.GetData(audioBuffer, 0, audioBuffer.Length)) > 0)
        audioStream.Write(audioBuffer, 0, bytesRead);

      MicrophoneStatus.Text = microphone.State.ToString();
    }

    private void recordButton_Click(object sender, RoutedEventArgs e)
    {
      if (microphone != null)
        microphone.Stop();

      audioStream = new MemoryStream();

      microphone.Start();
      MicrophoneStatus.Text = microphone.State.ToString();
}

    private void stopRecordingButton_Click(object sender, RoutedEventArgs e)
    {
      if (microphone.State != MicrophoneState.Stopped)
        microphone.Stop();

					  

audioStream.Position = 0;
      MicrophoneStatus.Text = microphone.State.ToString();
    }

    private void playButton_Click(object sender, RoutedEventArgs e)
    {
      SoundEffect recordedAudio =
        new SoundEffect(audioStream.ToArray(), microphone.SampleRate,
          AudioChannels.Mono);

      recordedAudio.Play(1f, (float)pitchSlider.Value, 0f);
    }
  }
}

With the Microphone class, developers can create fun applications that allow a user to record and playback recorded audio with pitch modifications.
Other -----------------
- Handling Input on Windows Phone 7 : Accelerometer
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 2) - Getting a Device
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 1) - Recreating the Project on Xbox, Devices and Containers
- XNA Game Studio 4.0 : Storage - Isolated Storage
- Using Media in XNA Game Studio : Visualizations
- Using Media in XNA Game Studio : Video
- XNA Game Studio 4.0 : Dynamic Sound Effects - Recording Audio with a Microphone, Generating Dynamic Sound Effects
- XNA Game Studio 4.0 : Playing Sound Effects (part 2) - Microsoft Cross-Platform Audio Creations Tool
- XNA Game Studio 4.0 : Playing Sound Effects (part 1) - Using SoundEffect for Audio Playback
- Windows Phone 7 : Using MVVM and Performing Unit Testing
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server