Problem
You want to play an MP3 or WMA audio file in xNa.
Solution
XNa can use the default content pipeline to load MP3 or WMA audio files to a song object. After loading, you can use the static mediaplayer class to play the song object.
Working Principle
First, add the MP3 or WMA file to the xNa project. This is the same as adding an image. See tutorial 3-1. The simplest way is to drag an MP3 or WMA file to the content folder.
You need to link a song object to an MP3 or WMA file.CodeAdd this object to the top:
Song mysong;
Use the content pipeline to load objects in the loadcontent method:
Mysong = content. Load <song> ("sample ");
You can use the following simple command to play a file:
Mediaplayer. Play (mysong );
Mediaplayer is a static class that can only play one sound at a time, which is ideal for playing background music. However, you need to play another sound in the game in a way similar to 7-1 in the tutorial.
The song object and the mediaplayer class both have some useful attributes, such as the length of the song and the playing progress of the current song:
Timespan Togo = mysong. Duration-mediaplayer. playpositon; string mytext = "time remaining for current song:" + Togo. tostring ();