Windows Phone recording audio

Source: Internet
Author: User

The Microphone access class provided by Windows Phone is Microsoft. Xna. Framework. Audio. Microphone, which belongs to the XNA Framework. This class is also required to access the Windows Phone Microphone in Silverlight. Therefore, you must add a reference to Microsoft. Xna. Framework.
1. Declare local variables to obtain the microphone Singleton.

// Mic Singleton
Private Microphone microphone = Microphone. Default;
// Capture the audio cache each time
Private byte [] buf;
// Audio stream storage Zone
Private MemoryStream audioStream;

2. every 33fp in XNA will update the screen once, but there is no such mechanism in Silverlight Application. To ensure that the recording function continuously updates the status and retrieves the status, you need to specify a regular execution of FrameworkDispatcher. update () event.

// Set the timer
DispatcherTimer timer = new DispatcherTimer ();
Timer. Interval = TimeSpan. FromMilliseconds (33 );
Timer. Tick + = delegate {try {FrameworkDispatcher. Update ();} catch {}};
Timer. Start ();

3. Set the recording information and start the recording.

// Set the cache for one second. If you do not obtain the audio for one second, a BufferReady event is called.
Microphone. BufferDuration = TimeSpan. FromMilliseconds (1000 );
// Allocate the cache for 1 second audio
Buf = new byte [microphone. GetSampleSizeInBytes (microphone. BufferDuration)];
AudioStream = new MemoryStream ();
// BufferReady event
Microphone. BufferReady + = new EventHandler <EventArgs> (microphone_BufferReady );
// Start the recording
Microphone. Start ();

3. Implement the BufferReady event handler. The handler copies the microphone data to the buffer and writes the buffer to a stream.

Void microphone_BufferReady (object sender, EventArgs e)
{
// Copy the microphone data to the buffer zone
Microphone. GetData (buf );
// Write the buffer to a stream
AudioStream. Write (buf, 0, buf. Length );
}

4. Stop recording

// Stop recording
Microphone. Stop ();
Microphone. BufferReady-= new EventHandler <EventArgs> (microphone_BufferReady );
AudioStream. Flush ();
// Convert the data stream to byte, which is the audio data in recording.
Byte [] recording = audioStream. ToArray ();
AudioStream. Dispose ();

5. Play the recording

// Play the recording
SoundEffect sound = new SoundEffect (audioStream. ToArray (), microphone. SampleRate, AudioChannels. Mono );
Sound. Play ();

The recorded byte stream data can be played through the SoundEffect class, which is quite easy. However, if you want to play the audio data in another way or want to play the audio data everywhere, you need to save the audio data to an independent storage as a file. Recording audio storage will be introduced later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.