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.