MP3
Last updated
Last updated
The MP3 file format often used for music. All information on this page also applies to the OGG file format. FlatRedBall loads MP3 files as Song instances. For more information on working directly with Songs, see the Song page.
To play an MP3 in your game:
Create or select the screen that you want to have the song
Expand the screen to see the Files item
Drag+drop the MP3 file from the explorer into a Screen's Files node.
Alternatively you can right-click on the Files tree node and select Add File->Existing File and browse to the location of the MP3
Note that by default the dropped song uses the Microsoft.Xna.Framework.Media.Song runtime type. See below for more information on this compared to other types.
The song automatically plays when the Screen starts. The song automatically stops playing when the Screen exits. You do not need write any code to play or stop the music. If a Screen contains multiple audio files, then additional settings and logic are needed to select which song should be played.
The song that is played uses the AudioManager.PlaySong method, so any properties set on the AudioManager (such as MasterSongVolume) apply to the playing of the song. For more information, see the AudioManager.PlaySong page.
FlatRedBall provides sample songs which you can use to test song playing. To add a new song to your game:
Right-click on the Files folder in a Screen
Select Add File -> New File
Search for Song
Select one of the song types and click OK
For more information on NAudio, see below.
FlatRedBall provides simple song controls in the Song tab. To view the Song tab:
Add a song file to your game (as shown above)
Select the newly-created song
Select the Song tab
By default a Song added to a Screen plays when the Screen starts and stops when the screen is destroyed. To have songs persist between Screen transitions, check the Keep Playing After Leaving Screen checkbox. Note that if this is checked, the song continues to play only if either of the following conditions are true:
The new screen does not specify a song to play
The new screen does specify a song to play, but the song is the same as the song that was playing in the previous screen
If the new screen specifies a different song, then the new screen's song begins playing even if the old song's Keep Playing After Leaving Screen checkbox is checked.
You can add multiple songs to your Screen if you would like to select which one to play in custom code. To do this:
Create a Screen
Add any number of .mp3 files to your Screen
Set each of them to LoadedOnlyWhenReferenced. For more information on this property, see the LoadedOnlyWhenReferenced page.
In custom code, play the song you would like to play. For example, if your song is called MySong, then add the following code:
For more information on the AudioManager, see the AudioManager page.
FlatRedBall provides built-in support for NAudio. When a new song is added through the Add File -> New File right-click option, a window allows the selection of NAudio or (MonoGame) Songs.
Already-added songs can be changed between Song and NAudio_Song by changing the runtime type on the file's properties.
Both MonoGame/FNA Song
and NAudio_Song
share much of the same functionality. Both:
Can be managed through the Song tab in FlatRedBall
Can be played through AudioManager.PlaySong
Respond to the AudioManager's MasterSongVolume if played through AudioManager.PlaySong
NAudio provides additional functionality for playing songs, so games which need more control over music playing may want to use NAudio_Song.