Async. loading of sounds

Monkey Forums/Monkey Programming/Async. loading of sounds

Midimaster(Posted 2013) [#1]
I want to change the loading of sound to the new async method. But I'm not sure, what is the best practice...

I have to load 42 sounds. Should I start all 42 loadings at the same time, or should I only start 1, the wait until it is finished, the start the next?


AdamRedwoods(Posted 2013) [#2]
'' ... in your main class...
Field soundLoader:SoundLoader
Field soundLoadStart:bool = false

'' ...in your main loader....
Method OnCreate()
  soundLoader = New SoundLoader(path, soundLoader)
End

Method OnUpdate()
  If not soundLoadStart Then soundLoader.Start(); soundLoadStart=true '' call this once
  UpdateAsyncEvents()
  '' ... etc...
  
  '' ...later on...
  Local mysound:Sound = soundLoader.GetSound( path )
End

''...elsewhere...
Class SoundLoader Extends AsyncSoundLoader Implements IOnLoadSoundComplete
  Field sounds:StringMap = New StringMap

  Method OnLoadSoundComplete (sound:Sound,path:String,callback:SoundLoader)
    '' flag sound as loaded
    sounds.Set(path,sound)
  End

  Method GetSound:Sound(path:String)
    ''if this returns null, it wasn't loaded
    Return sounds.Get(path)
  End
End

so i didn't test this, just did it off my head. this example doesn't check for files not found, but could be added by throwing a flag or throwing an exception, then using a try/catch around the UpdateAsyncEvents.