Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
According to this you need to create a media library to store all the songs instead of trying to play individual songs: msdn.microsoft.com/en-us/library/dd231914.aspx that way you can also just set the media player to Shuffle so that you don't need to change tracks yourself. – Roy T. Dec 24 '12 at 10:57

closed as too localized by Tetrad Feb 22 at 17:23

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

Sorry, the tracks are 1:20 long. Therefore the duration's should be 80. Simple mistake.

I'd still love to hear a better way to get the duration's though.

share|improve this answer
you know you can edit your question to add those details. Adding an answer is not really the way to go, as this is not an "answer". – Zonko Dec 25 '12 at 23:57
It is in fact the answer. I was setting the durations to 120 seconds rather than 80 seconds. But as I stated I would still like to know of a better way to do this if it exists. – Josh Siegl Dec 26 '12 at 7:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.