XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Detecting Song delay

Last post 10/21/2009 7:35 PM by David Hunt. 5 replies.
  • 10/19/2009 5:58 PM

    Detecting Song delay

    How would I do this? Basically I have a funciton spawnStuff();, and I want to call that function when the song actually starts playing. I'm using MediaPlayer.Play(soundtrack);. Any ideas?
  • 10/20/2009 6:57 PM In reply to

    Re: Detecting Song delay

    Why not right after you call MediaPlayer.Play(soundtrack); then just call your spawnStuff() method immediately after?

    if your song doesn't start immediately and if that is your problem, then you can store a field to keep track of the time since you called your Play(soundtrack) method and if you load up your song in any player you can take note of how many seconds after play that you want your spawnStuff() to happen.  Once you know how many seconds, then you can check your stored field until it reaches that many seconds, then call your spawnStuff() like so.

    float milliseconds = 0f;

    bool isSpawnStuffCalled = false;

    public void Update(GameTime gameTime) {
        
         //this starts counting so put this line just after you call your MediaPlayer.Play(soundtrack)
         milliseconds += gameTime.ElapsedGameTime.Milliseconds;

         //this is how you can check how many seconds passed, each 1000 milliseconds is equal to 1 second of course :)
         if(isSpawnStuffCalled == false)
         {
              if(milliseconds > 5000) {
                   spawnStuff();
              }
         }
    }
    public void SpawnStuff()
    {
         //set the boolean so that your spawnStuff doesn't happen over and over forever
         isSpawnStuffCalled = true;

         //do your spawn stuff here
    }
  • 10/20/2009 6:58 PM In reply to

    Re: Detecting Song delay

    I'm new to programming with XNA also and I'm not too sure how the pro's around here would do it, but the code above should work.
  • 10/20/2009 8:40 PM In reply to

    Re: Detecting Song delay

    Answer
    Reply Quote
    I'm not 100% sure when the MediaPlayer State property is set, but you could trying polling MediaPlayer.State until it equals MediaState.Playing. If the state isn't actually set to playing until the song starts, that would get you what you want. If it's set at the time you call Play, then you're out of luck there.

    Another possibility would be to poll the MediaPlayer.PlayPosition property until it is non-zero.
  • 10/21/2009 7:21 PM In reply to

    Re: Detecting Song delay

    David Hunt:

    Another possibility would be to poll the MediaPlayer.PlayPosition property until it is non-zero.


    Aha, that worked nicely, thanks.
  • 10/21/2009 7:35 PM In reply to

    Re: Detecting Song delay

    Glad I could help!
Page 1 of 1 (6 items) Previous Next