Author Topic: Preloading Sound Files  (Read 3974 times)

Jam0864

  • Contributor
  • Fanatic
  • **
  • Posts: 744
    • MSN Messenger - marmalade0864@hotmail.com
    • View Profile
    • Jam0864's Content Dump
    • Email
Preloading Sound Files
« on: 2009-04-10, 08:40:54 PM »
The first time a sound is played it is delayed since it's not preloaded. For the project I'm working on, it's crucial that it is preloaded.

I tried preloading all the sounds at startup by playing them all at once, but that crashed the project.

Is there something I can do the preload the sounds?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Sound Files
« Reply #1 on: 2009-04-11, 09:40:17 AM »
Did you try the LoadSound function?  It should be an option not far from PlaySound.

Jam0864

  • Contributor
  • Fanatic
  • **
  • Posts: 744
    • MSN Messenger - marmalade0864@hotmail.com
    • View Profile
    • Jam0864's Content Dump
    • Email
Re: Preloading Sound Files
« Reply #2 on: 2009-04-11, 07:45:53 PM »
Didn't see that.  :-[

Thanks. ;)

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Preloading Sound Files
« Reply #3 on: 2010-01-12, 01:00:04 PM »
Hey guys, I know I'm late on this topic, but maybe you can help me out.

In my game, it's not 100% necessary to preload the sounds, but it sure would be nice if the game didn't stop each time a new music is loaded.  So I did like Jam0864 and I called LoadSound() for each music I have.  The thing is, I've got 12 mp3 and all together they weight around 45 megs.  I compressed them as much as I could usign Audacity and the Lame mp3 encoder.  It used to be 75 megs, so I'm pretty happy it dropped to 45 but it's still huge. 

I was wondering if there was a way to load the music asynchronously, so when the game starts, I would load every music and they will play when they are loaded without any freeze.  If a music is supposed to be played when it is not loaded, the game would go on without music until it is loaded or it would stop until this precise music is loaded, whichever is easier to do.

Any suggestions?  Can someone help me out with this?

Thanks!  ;D
« Last Edit: 2010-01-12, 03:29:59 PM by Vincent »
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Sound Files
« Reply #4 on: 2010-01-12, 03:31:17 PM »
So what's the problem with using LoadSound on your current project's sounds?  Does it take too long with 45 MB of sounds, or does it take too much memory or what?

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Preloading Sound Files
« Reply #5 on: 2010-01-12, 03:39:22 PM »
It's very long.  It was already taking quite long before I added the music, but now it's over the top.   At least it doesn't crash or freeze.
« Last Edit: 2010-01-12, 07:16:11 PM by Vincent »
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Sound Files
« Reply #6 on: 2010-01-13, 06:12:25 AM »
You could try creating functions "StartLoadSound" and "EndLoadSound" which would be similar to many asynchronous functions in the .NET framework.  The "Start" function begins a background thread to run the process, but returns immediately after the thread is started.  The "EndLoadSound" returns the final result of the thread when it is complete.  StartLoadSound would return an object which can be passed to EndLoadSound so that EndLoadSound knows which sound/thread you are referring to.  This object can also be used to check whether the thread is ready to finish yet.  Hm, it looks like .NET even provides a generic way to call a function asynchronously.  See http://msdn.microsoft.com/en-us/magazine/cc301332.aspx.  You may want to start with the conclusion and work your way backwards until you find what you need :).  (There's a lot of introduction)

I guess the key is knowing how to use BeginInvoke and EndInvoke.  http://shiman.wordpress.com/2008/09/10/c-net-delegates-asynchronous-invocation-begininvoke-method/

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Preloading Sound Files
« Reply #7 on: 2010-01-13, 08:13:03 AM »
Hey, thanks a lot for pointing this out for me! :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com