After a few false starts, I’ve got FMOD Studio working with my current project, and I must say the DAW interface is fun to work with; plus, being able to tinker while the game is running is pretty great. Unfortunately, though, I seem to be doing something wrong in my implementation - every sound takes around a quarter second to actually play. For example, when the player collects a key, the “Get Key” sound effect starts playing noticeably late.
The following is my simple “Play Sound” function, which allows the programmer to pass in a string such as “event:/key” to choose what event to trigger.
void AudioSys::PlaySound(std::string soundToPlay, float volume)
{
if (IsAudioOn)
{
const char * sound = soundToPlay.c_str();
FMOD::Studio::ID eventID = {0};
ERRCHECK( fmodSystem->lookupID(sound, &eventID) );
if (LastSoundPlayed == &eventID)
if (soundRepeatDelay != 0.0f)
return;
// One-shot event
LastSoundPlayed = &eventID;
FMOD::Studio::EventDescription* eventDescription = NULL;
ERRCHECK( fmodSystem->getEvent(&eventID, FMOD_STUDIO_LOAD_BEGIN_NOW, &eventDescription) );
FMOD::Studio::EventInstance* eventInstance = NULL;
ERRCHECK( eventDescription->createInstance(&eventInstance) );
eventInstance->setVolume(volume);
ERRCHECK( eventInstance->start() );
soundRepeatDelay = SOUND_REPEAT_DELAY;
// Release will clean up the instance when it completes
ERRCHECK( eventInstance->release() );
}
}
I’m not sure what I could be doing wrong, as I don’t know Studio well enough to spot the problem, but I greatly appreciate any help you guys can offer!
It sounds like your sample data may be taking a while to load. The sample data for an event starts loading asynchronously when you call EventDescription::createInstance(), so if you call EventInstance::start() immediately, playback may be delayed until the sample data is loaded.
To play audio without a delay, you need to wait until the sample data is loaded before calling EventInstance::start(). You can do this in one of two ways:
[list]
[]Wait until the sample data is loaded for that specific event by polling EventInstance::getLoadingState() until it returns FMOD_STUDIO_LOADING_STATE_LOADED[/ptx21uox]
[]Load sample data for all events in the bank by calling Bank::loadSampleData() and poll Bank::getSampleLoadingState() until it returns FMOD_STUDIO_LOADING_STATE_LOADED[/ptx21uox][/list:u:ptx21uox]
However, it seems to hang the program - indefinitely. Even with a small set of sounds that shouldn’t take long to load, the while loop doesn’t seem to end. Am I missing something crucial again? Thanks for your help in advance!