I'm rather new to working with music in XNA.
I have a class called SoundManager which handles playing music. Here it is:
public class SoundManager
{
private List<Song> Songs;
private List<float> Durations;
private Random random;
private int CurrentSong = 0;
private float TrackTimer = 0f;
public SoundManager()
{
Songs = new List<Song>();
Durations = new List<float>();
random = new Random();
CurrentSong = random.Next(0, 2);
Songs.Add(Sounds.EconomicProsperity);
Durations.Add(120f);
Songs.Add(Sounds.BusyCity);
Durations.Add(120f);
MediaPlayer.Play(Songs[CurrentSong]);
}
public void UpdateSoundManager(GameTime gametime)
{
TrackTimer += (float)gametime.ElapsedGameTime.TotalSeconds;
if (TrackTimer >= Durations[CurrentSong])
{
MediaPlayer.Stop();
int newTrack = CurrentSong;
while (newTrack == CurrentSong)
{
random = new Random();
CurrentSong = random.Next(0, 2);
}
MediaPlayer.Play(Songs[CurrentSong]);
TrackTimer = 0f;
}
}
The Sounds object is just a static class that holds all of the loaded Audio, basically "BusyCity" and "EconomicProsperity" are just loaded song objects.
The problem I'm having is in the UpdateSoundManager(Gametime gametime) method.
I have a private float called TrackTimer which is used to get the number of seconds each track is being played for, which is then checked against my list of floats called Durations
Note this probably isn't the best way of switching tracks (if anyone could give me a better way I'd be thrilled)
I then grab a new track by getting a new random (while checks to make sure it doesn't repeat)
I then try to play it through MediaPlayer.Play(Songs[CurrentSong]) but nothing is happening.
What am I doing wrong? Am I using MediaPlayer in the wrong way? How do I switch to a new song? Is it a problem with how I'm checking the duration's of each song?
I've checked the MSDN documentation but I'm stumped, any help is greatly appreciated.
EDIT: Just for clarification all i'm trying to do is switch to a new track. The first track plays fine it just doesn't seem to want to play the other one using my method.
