Problem
You want to loop the sound, such as playing background music or playing continuous sound, such as a car engine sound.
Note:Because Zune does not support xact, you need to use the reload method of soundeffect. Play () to play the audio cyclically. See the corresponding explanation in 7-1 of the tutorial.
Solution
The xact audio tool can easily indicate whether a sound needs to be played cyclically. You willCodeCreate a cue object, because you need to be able to pause or stop during playing.
You can also check whether a sound has been played, which is important when you want to switch the background music. This operation can be done by checking the isstopped attribute of cue.
Working Principle
Loop playback of a sound you can set the loopevent attribute of a sound to infinite in xact to loop playback. To do this, open your xact project and select the sound to be played cyclically in the sound bank. After the sound is selected, the play wave in the upper-right corner of the sound bank panel will become visible. Click the play wave node, as shown in 7-2.
After clicking the play wave node, its properties are visible in the Properties window at the bottom left of the xact window. Find the loopevent attribute and set it to infinite. Do not forget to save the xact project.
Figure 7-2 when sound is selected, the play wave node becomes visible.
Now, when you re-compile the xNa project and use the previous code to play the cue, it will play infinitely. You can stop playing this cue when appropriate because you want to control it. Therefore, you need to create a cue object to store reference to this cue:
Cue cue1;
Call the getcue method of the soundbank variable to assign values to this variable. You can call the play method to play the cue:
Cue1 = soundbank. getcue ("audio1"); cue1.play ();
This will make cue loop playback, Which is set in xact audio tool. But this time, you have a reference pointing to cue, so you can pause, continue, or stop this cue:
Cue1.pause (); cue1.resume (); cue1.stop (audiostopoptions. Immediate );
Beware:After you stop a cue from playing, you cannot simply call play again on this cue. You must call the getcue method to call it again from soundbank.
Check whether a sound has stopped playing or changed the background music.
You can check whether a sound cue has been played by using the isstopped attribute of sound:
If (currentcue. isstopped) // do something
If you want xNaProgramTo play background music cyclically, you need to create an array to save the name of cue instead of the cue itself. When cue starts playing, cue itself becomes useless. You need some additional variables to create a background loop system:
String [] bgcuenames; cue currentcue; int currentcuenr = 0;
This array stores the name of the background cue to be played, currentcue stores the currently playing Cue, so you can check whether it has ended, and the currentcuenr variable is used to activate the Next cue.
The following method initializes the Cun name array and starts the First cue:
Private void initsounds ()... {audioengine = new audioengine ("content/Audio/myxactproject. xgs "); wavebank = new wavebank (audioengine," content/Audio/mywavebank. xwb "); soundbank = new soundbank (audioengine," content/Audio/mysoundbank. xsb "); bgcuenames = new string [5]; bgcuenames [0] =" bgaudio1 "; bgcuenames [1] =" bgaudio2 "; bgcuenames [2] =" bgaudio3 "; bgcuenames [3] = "bgaudio4"; bgcuenames [4] = "bgaudio5"; playbgcue (0 );}
First, you need to create an xact project, as shown in tutorial 7-1, which contains five cues. The playbgcue Method for starting a cue is simple:
Private void playbgcue (INT cuenr)... {currentcue = soundbank. getcue (bgcuenames [cuenr]); currentcue. Play ();}
It stores the reference of the currently playing Cue in the currentcue variable, so you can check its isplayed attribute in the updatesounds method, and the updatesounds method is placed in the main update method:
Private void updatesounds ()... {If (currentcue. isstopped )... {If (++ currentcuenr = bgcuenames. length) currentcuenr = 0; playbgcue (currentcuenr);} audioengine. update ();}
If the current cue has been played, increase currentcuenr. If it is greater than the number of cue to play (5 in this example), set it to 0. Finally, playbgcue is called to play the next sound.
Note:++ Before currentcuenr indicates that you want to increase the value of currentcuenr before the value is evaluated. If it is written as currentcuenr ++, when currentcuenr is set to 4, the system determines that the result is false. If the value is set to 5, an outofrange error of the getcue method is thrown. By writing it as ++ currentcuenr, if currentcuenr is 4, this value is first increased to 5, so that the result is true, and this value is set to 0.
By placing the update method of audioengine in this method, everything called by update is the updatesounds method.
Code
In the previous chapter, you can find all the code for Loop Playing the background music list. The following code starts Loop Playing the sound defined in the first part of this tutorial by pressing the Space key, and press enter to stop:
Protected override void Update (gametime )... {gamepadstate = gamepad. getstate (playerindex. one); If (gamepadstate. buttons. back = buttonstate. pressed) This. exit (); keyboardstate keystate = keyboard. getstate (); If (keystate. iskeydown (keys. space) | (gamepadstate. buttons. B = buttonstate. pressed ))... {If (cue1 = NULL) | (cue1.isstopped ))... {cue1 = soundbank. getcue ("audio1 "); Cue1.play () ;}} if (keystate. iskeydown (Keys. Enter) | (gamepadstate. Buttons. A buttonstate. Pressed) if (cue1! = NULL) cue1.stop (audiostopoptions. Immediate); audioengine. Update (); base. Update (gametime );}
When the Space key is pressed for the first time, the cue1 variable is null. Once you press enter, cue stops. When you press the Space key again, the cue will play back.
Again, you need to re-create the cue1 variable after cue is stopped (by calling the getcue method ).