In the previous chapter, we made a lot of dynamic effects. As a game, apart from visual effects, we also needed music effects. This article will add some sound effects to the game. For example, music prompts will be sent when an item is found, and background music will be generated during the game, so that the game can be more perfect and realistic.
Let's take a look at the effect after adding sound effects (because loading of music files will be slower ):
1First, add two music files toAudioFolder, and set build actionContentIn this wayProgramThe music file is encapsulated in the xap file:
2. Next, addPlaysoundactionWhen you click on a product, the sound will pop up. (magic_wand.mp3 ):
Set playsoundaction as follows, and select SourceMagic_wand.mp3File:
Similarly, you can copy playsoundaction to another 12 paths and run the program to see how it works.
3. Finally, let's make the background music. Wounds of the past is a song in the original sound album of ghost WARRIOR 3. It is a mysterious and miserable background music suitable for this game, what's more, the size is relative to 4 ~ 5 MB files are relatively small, and the loading speed will be faster during demo.
To put it bluntly, you must first add a mediaelement control in layoutroot and name itMusicmediaelementUsed to play background music:
Select wounds_of_the_past.pdf in the media Repository:
4After running the program, you will find that the background music is played only once. In fact, we want the music to be played cyclically. To achieve this function, create a behavior for loop playback and create it in interactivity.ContinuousplaymediabehaviorFolder and addContinuousplaymediabehavior. CS:
The key to this behavior is associatedobject_mediaended. After the music ends, it sets the music time to 0 and plays it again:
Public class Continuousplaymediabehavior : Behavior < Mediaelement > { Public Continuousplaymediabehavior (){} Protected override void Onattached (){ Base . Onattached (); associatedobject. mediaended + = associatedobject_mediaended ;} Protected override void Ondetaching (){ Base . Ondetaching (); associatedobject. mediaended-= associatedobject_mediaended ;} Void Associatedobject_mediaended ( Object Sender, Routedeventargs E) {associatedobject. Position = New Timespan (0); associatedobject. Play ();}}
5. Add continuousplaymediabehavior to musicmediaelement to achieve the loop playback effect:
Source codeDownload: