Array

Using XNA Interop to Record Audio

Must Read

Los Angeles Private Investigator

"Badges? We ain't got no... stinking badges!" I bet you've heard that before. I remember a high school friend...

Bad Credit Home Equity Loans – Tips On Obtaining A Home Equity Loan Even if You Have Bad Credit

Consider this scenario: You have an urgent, large expense to meet. You want to consider a home equity loan...

Government Assisted Consolidated Loans – The Free Federal Program to Consolidate Your Debt

If you have a pile of federal student loans you most certainly will want to become educated on how...
Admin
test

Before the Tailspin mobile client application can handle events raised by XNA objects, such as a Microphone object, it must create an XNA asynchronous dispatcher service. The following code example from the App.xaml.cs file shows how this is done.

C#
public App()
{

this.ApplicationLifetimeObjects.Add(
new XnaAsyncDispatcher(TimeSpan.FromMilliseconds(50)));
}

The VoiceQuestionView.xaml file defines two buttons, one toggles recording on and off, and the other plays back any saved audio. The recording toggle button is bound to the DefaultAction Command command in the view model, and the play button is bound to the PlayCommand command in the view-model.

The DefaultAction command uses the StartRecording and StopRecording methods in the VoiceQuestionViewModel class to start and stop audio recording. The following code example shows the StartRecording method.

C#
private void StartRecording()
{
var mic = Microphone.Default;
if (mic.State == MicrophoneState.Started)
{
mic.Stop();
}
this.formatter = new WaveFormatter(
this.wavFileName, (ushort)mic.SampleRate, 16, 1);
this.observableMic = Observable.FromEvent<EventArgs>(

h => mic.BufferReady += h, h => mic.BufferReady -= h)
.Subscribe(p =>
{
var content =
new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];
mic.GetData(content);
if (this.formatter != null)
{
this.formatter.WriteDataChunk(content);
}
});
mic.Start();
}

This method gets a reference to the default microphone on the device and creates a WaveFormatter instance to convert the raw audio data to the WAV format.

The method uses the Observable.FromEvent method to subscribe to the microphone’s BufferReady event, and whenever the event is raised, the application uses the WaveFormatter instance to write the audio data to isolated storage. Finally, the method starts the microphone.

The following code example shows the StopRecording method that disposes of the Microphone and WaveFormatter instances and attaches the name of the saved audio file to the question.

C#
private void StopRecording()
{
Microphone.Default.Stop();
this.observableMic.Dispose();
this.formatter.Dispose();
this.formatter = null;
this.Answer.Value = this.wavFileName;
}

The play button in the VoiceQuestionView view plays the recorded audio by using the SoundEffect class from the Microsoft. Xna.Framework.Audio namespace. The following code example shows the Play method from the VoiceQuestionViewModel class that loads audio data from isolated storage and plays it back.

C#
private void Play()
{
this.IsPlaying = true;
using (var fileSystem =
IsolatedStorageFile.GetUserStoreForApplication())
{
using (var dat = fileSystem.OpenFile(
this.wavFileName, FileMode.Open, FileAccess.Read))
{
try
{
using (var effect = SoundEffect.FromStream(dat))
{
var instance = effect.CreateInstance();
instance.Play();
while (instance.State == SoundState.Playing)
{
System.Threading.Thread.Sleep(100);
}
}
}
catch (ArgumentException)
{
}
}
}
this.IsPlaying = false;

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits, even though it will have...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

More Articles Like This