SoundEffect for playing audio on Windows Phone

Source: Internet
Author: User

This section describes how to record audio on Windows Phone and how to save audio on Windows Phone. The recorded audio is saved in WAV format. There are many ways to play audio in Windows Phone. The following describes a playback method specifically used to play audio in WAV format. The SoundEffect and SoundEffectInstance classes are required. These two classes belong to the XNA Framework. Therefore, you must add Microsoft. Xna. Framework references.
1. Like recording audio, you must specify an event that periodically executes FrameworkDispatcher. Update.

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

2. Obtain the WAV file stream to create a SoundEffect object.

// Obtain the WAV file stream
Stream stream = null;
// For resource file processing
StreamResourceInfo info = Application. GetResourceStream (new Uri (path, UriKind. Relative ));
If (info! = Null)
{
Stream = info. Stream;
}
// If it is an independent storage file processing
Using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile. GetUserStoreForApplication ())
{
// Open the file
Stream = myIsolatedStorage. OpenFile (path, FileMode. Open, FileAccess. Read );
}

3. Create a SoundEffect object to play the audio.

// Create an audio playback instance
SoundEffect sound = SoundEffect. FromStream (stream );
SoundEffectInstance soundInstance = sound. CreateInstance ();
// Set loop playback
SoundInstance. IsLooped = true;
// Start playing
SoundInstance. Play ();

4. Pause, reset, and stop the audio.

// Pause
SoundInstance. Pause ();
// Reset
SoundInstance. Resume ();
// Stop
SoundInstance. Stop ();

5. Set the audio playback volume.

// The volume value ranges from 0 to 1. The default value is 0.85.
SoundInstance. Volume = 0.5F;

You can only play audio in WAV format using SoundEffect and SoundEffectInstance, and you also need to introduce the XNA library.

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.