Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Microsoft Silverlight 5 is a rich application framework for creating high-performance, cross-platform desktop and mobile applications. In this course, author Walt Ritscher demonstrates how to build a variety of applications in Silverlight, with particular focus on building compelling business applications and delivering premium video and audio content. Developers will work with the C# programming language and Visual Studio Professional, as well as Expression Blend, a tool that simplifies creation of the interactive user interfaces expected in modern-day applications.
There are times when you need to play a sound effect or other short background noise. Now that Silverlight has XNA support, you can play effects through the XNA libraries. For this demonstration, I am using Visual Studio and a project called UsingSoundEffects. Before you can use the SoundEffect classes, you need to add a reference to the XNA framework library. Silverlight is leveraging the work done by the XNA team for graphics and sound. They do this by interoping with the XNA libraries. To play an effect, we use the SoundEffect class.
The MediaElement class has great support for playing audio files. Why have another class? Well, the MediaElement is optimized for streaming music to the application. That might be good for listening to your favorite song, but playing a buzz or a XAP effect needs to happen instantaneously. No latency allowed. Typically, your effect files are stored as content or resources in your app, so they are available on client computer. In my project, my effect files are in this Sounds folder. I have four of them. Sound effects must be in the WAV format.
I am going to start by playing this ufo.wav file. To do that, I am going to go to MainPage.xaml and then double-click on the first button. I will start by creating a streamInfo variable. I am calling the application class .GetResourceStream and I am telling it to load my ufo.wav file. Next, I go to the SoundEffect class and I ask it to load a stream of information.
Once I have my effects, I call the Play method. Just that simple! Press F5 to run the application and click on this first button. (sound effect) I like that. Let's hear that again. (sound effect) To get more control over the effect and reduce stress on the application memory, you can use the SoundEffectInstance class. It works in alliance with the SoundEffect class. You create the SoundEffect as usual. When you need to play the sound, you use the SoundEffectInstance class to get a copy for the job.
There are several advantages to this. You can play up to sixteen simultaneous copies of the sound this way. One other advantage is that you can loop the instance. Casual computer games use this technique. They might have a 30-second music loop playing during gameplay. For my SoundEffectInstance demo, I am going to open up another file. I am going to open this UseSoundEffectInstance. I am also going to change my start up object in App.xaml. Now, I will open the UseSoundEffectInstance.xaml file.
The UI in this example has a checkbox at the top for playing the music and not playing the music. I will loop a sound effect file for this. I have these for playing a note at different pitches and then when you click on the background of this image, I am going to play an applause track while my bicyclist rides across the screen. Go to code behind. Here, I am using the same technique I showed you a few seconds ago. I am calling GetResourceStream and loading the applause.wav file.
Then I am loading the effect, and then I am calling the BikeStoryboard.Begin(). When the user clicks on the UserControl, I am going to create an instance of the sound effect. I do that by going to the _effect class, and calling the CreateInstance. Now, I have stored that instance in this variable, and I can do things like panning and pitch and of course, play. Here is the demonstration. As she rides across the screen, (applause) I will click down the canvas, and you can hear the applause as she races off the edge of the screen.
Now, I mentioned you could loop music. Let me show you a demo of that, and then I will go and show you the code. (music playing) So every time I click that checkbox, it's playing the same two bars music over and over again. Also, you can do pitch play, when I can click on these rectangles and get a pitched piano sound. Let's show you the code.
Here is the RepeatMusic code. It's quite simple. Once you've created your instance, you set the IsLooped property to true and then play the song. When I uncheck the checkbox, then I stop the music, and I dispose off my instance by calling the dispose method. Next time you click on the checkbox, it creates the instance again and play it. As far as pitched play goes, it's quite simple as well. Here is a Pitch property. The value for Pitch can be a value between -1 and +1. So 0 means normal pitch. +1 means it's 1 octave above the normal pitch.
SoundEffectInstance is a good way to play background music in your application, as long as you remember that the music file must be a WAV file.
Find answers to the most frequently asked questions about Silverlight 5 Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.