Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The Play method can be used to play a SoundEffect, optionally specifying the volume to play at. The Play method performs the following additional logic:
It only plays the SoundEffect if AreSoundEffectsEnabled is true.
It checks if the SoundEffect has already been played this frame, preventing the same SoundEffect from playing twice in one frame. This behavior depends on the value of SoundEffectPlayingBehavior.
It increments NumberOfSoundEffectPlays which can be used to measure how many times sound effects have played.
It checks if the argument SoundEffect has been disposed (debug only) and throws an informative exception if so.
The following assumes that Explosion is a SoundEffect. This can be created by adding a .wav file to Glue.
Sounds can be played with a custom volume:
AreSoundEffectsEnabled can be used to make the Play function produce no audio. This is often set by options screens so that the game code does not need to be cluttered with if-checks surrounding all audio playing.
Setting AreSoundEffectsEnabled to false will not stop any already-playing SoundEffects.
The PositionedSound class is used to play a sound using the 3D audio features of XACT. This means a sound can have a position and velocity, which will be translated into volume and pitch levels on all available speakers.
A PositionedSound is a combination of a Sound class and a PositionedObject. This means that all the sound playback controls of the Sound class work the same way in the PositionedSound class, and all methods available in the PositionedObject class are also available to the PositionedSound class. This opens up some great posibilities, like this example:
Then, whenever the ship fires, a simple call to shipFireSound.Play() will play the sound in the appropriate position.
To have sounds positioned correctly, the engine must know where you want the listener to be hearing the sounds from. The AudioManager exposes a SoundListener field to control this. The SoundListener is a positioned object, so it can be placed and rotated just like other objects.
For most situations, attaching the SoundListener to the default Camera will work just fine. This line of code will do just that:
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
MasterSongVolume is the default value used when calling PlaySong. This value can be null if no default is used, or a value between 0 and 1 can be assigned where 1 is full volume and 0 is silent.
Setting MasterSongVolume sets the currently playing song's volume as well if any of the following are true:
The Song is a MonoGame/FNA Song which has been played through the AudioManager's PlaySong method
The Song is a MonoGame/FNA Song which has been played through the XNA MediaPlayer
The Song is an ISong (such as an NAudio_Song) and has been played thorugh the AudioManager's PlaySong method.
Setting MasterSongVolume does not affect the volume of NAudio_Song instances which have been played through the NAudio_Song's Play method. For more information, see the NAudio_Song page.
PlaySong plays the argument song, optionally restarting it if it is already playing. This method is automatically called if a song file is added to the FlatRedBall Editor, but it can also be explicitly called for more control over song playing.
Microsoft.Xna.Framework.Media.Song is the default class in MonoGame and FNA for playing music. PlaySong ultimately uses the MediaPlayer.Play method to play a song, so it is inherits the limitations of MediaPlayer, including being able to play only one song at a time. MonoGame's MediaPlayer also suffers from noticeable seek time on MP3s, including looping.
The PlaySong method can also receive any class which implements ISong. The most common type of ISong implementation is the NAudio song. NAudio provides additional flexibility and better seeking/looping compared to MonoGame and FNA, so it is recommended for games which require more song playing flexibility.
For more information on loading an NAudio_Song intance, see the MP3 loading page.
The Sound class is used to manage and play a sound cue. It can be obtained from the AudioManager using the GetSound(CueName) method.
The following methods will modify the playback of a sound:
Play() - Plays the sound, resumes the sound (if paused), or restarts the sound (if stopped).
Stop() / StopAsAuthored() - Stops playback of the sound as authored in the XACT project.
StopImmediately() - Stops playback of the sound immediately.
Pause() - Pauses playback of the sound.
Additionally, there is a Variables field within the sound object which provides easy access to any cue instance variables. For example, if the XACT project defined a "Volume" cue instance variable, then it could be accessed using the following line:
It could be set using the following line:
For more information on setting variables in an XACT project, read the XNA Creator's Club's Advanced Audio Tutorial on controlling pitch and volume with variables.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.