How to play a Song / music
Demonstrates how to play a song from a media library.
Overview
In this example we will demonstrate the ability to load a media file such as an MP3, OGG or WMA file.
The MediaPlayer class manages long running music or media. Will start audio playback when using the Play method. If the user currently is playing a different song, the Stop method can be used to stop the current song.
Note
Only one song can be played at any time using the MediaPlayer.
Requirements
This sample uses a mp3 file named rock_loop_stereo.mp3, which you can download from this link here. (save-as) But you can use your own if you wish.
To add a music file to your project
Open your MonoGame game in your editor of choice.
Open the
Content.mgcb
file by either double-clicking on it, or right-clicking and selecting `Open in MGCB editor". (depending on your editor)Click Add in the menu bar, and then click Existing Item. (alternatively, use the icon for the same task in the editor)
Navigate to the music (mp3) file you want to add, and then select it.
The selected audio file is inserted into your project. By default, it is processed by the Content Pipeline as a Song
To play a song
The following demonstrates how to play a song from the media file in the content project.
Declare a Song parameter as a reference for the media file to load.
private Song song;
Load the media file using Content.Load in the 'LoadContent' method.
song = Content.Load<Song>("rock_loop_stereo");
Check if the MediaPlayer is currently playing (or has a paused track), and then call "Stop" if needed. As soon as the MediaPlayer is ready, play the music.
// check the current state of the MediaPlayer. if(MediaPlayer.State != MediaState.Stopped) { MediaPlayer.Stop(); // stop current audio playback if playing or paused. } // Play the selected song reference. MediaPlayer.Play(song);
See Also
-
Provides a high-level overview about the capabilities—such as playing music and video and accessing pictures—of the Media API in MonoGame.
Reference
-
Provides methods and properties to play, pause, resume, and stop songs. MediaPlayer also exposes shuffle, repeat, volume, play position, and visualization capabilities.