The FromUri method games to create songs from a regular song file (such as .mp3) rather than relying on the Content Pipeline. If you are simply interested in playing a song, the easiest way to load and play a song is through Glue, as explained on this page. The FromUri method requires writing code.
The FromUri method provides the following benefits:
Does not require the usage of the Content Pipeline when loading songs in code.
Does not require adding content to the Visual Studio project, which is important if your project does not ship with all of the content it will use. Games which allow downloading DLC or modding will want to use the FromUri method. Furthermore, audio may be downloaded after a game is installed on Android to fit within the current 50 megabyte .apk limit.
Using the FromUri code requires loading a Song in custom code (as oppose to using Glue code to load the file). Song files which are loaded with FromUri can be added as follows:
Glue files can be loaded, but they must have their LoadedAtRuntime value set to false. For more information, see the LoadedAtRuntime page.
Files can be added manually to the Visual Studio/Xamarin Studio project manually.
Files can be added by the project itself, such as by unzipping a file or downloading a file from the Internet.
Once the file is available by the project, it can be loaded as follows:
The FromUri method as implemented in MonoGame 3.4 includes a bug requiring a workaround:
The Song class can be used to play music in a game. The easiest way to play a song is through Glue, which requires no code. For more information on Songs in Glue, see .
WAV files are not supported in FlatRedBall for music files. Instead, they are used by the and classes.
To play a song in Glue, drag+drop a file from disk into your Glue project on a Screen. It will play automatically when the screen loads. For more information about playing songs in Glue, see the .
To play a song:
Drag a music file (MP3 or WMA) into your project's Content folder through Visual Studio.
Add the following code:
Add the following using statements:
Add the following to Initialize after initializing FlatRedBall:
This error can occur in a number of cases:
If you're using a WMA/MP3 with DRM
If Windows Media Player is not installed on the computer running the game
Open Control Panel
Select Programs and Features
Turn windows features on or off
Expand Media Features
Make sure Windows Media Player is selected
OGG
This is dependent on the Android phone. Just because it plays on one phone doesn't mean it will play on another. If the phone doesn't have the codec, it will return a
The MediaPlayer class is an XNA class which controls the playing of music. For full documentation, see .
To have your game loop its music, add the following line of code to your Game1.cs file:
The MediaPlayer offers an overall volume value. This volume value applies to all music. In other words, if you set this value, then change the song that is being played, the same volume value will apply to the new song.
This makes Music play at full volume:
This makes Music play at half volume:
This makes Music play at 0 volume (effectively mutes the music):
Did this article leave any questions unanswered? Post any question in our for a rapid response.
This code uses the Content Pipeline: Notice that when this file is loaded you do not include the extension. This is because the file needs to be added to your project using the Content Pipeline. For more information on what the Content Pipeline is, see .
If attempting to play an .ogg file on the PC, you need to have the proper "DirectShow Filters" installed. To verify if you have them installed, attempt to play the .ogg file in Windows Media Player. If Windows Media Player does not recognize the file then you need the filters: If the song does not play, you can install the DirectShow Filders, .
Did this article leave any questions unanswered? Post any question in our for a rapid response.
Format | PC Desktop | Android |
MP3 | X | X |
WMA | X |
OGG | X |
X