I understand it and I know preload is asynchronous.
Thing is playing any sound before it loaded shoudn’t so drastically increase loading time.
Let me show you a little function:
void HelloWorld::preloadSounds(Ref* ref, ui::Widget::TouchEventType touchEventType) {
if(touchEventType != ui::Widget::TouchEventType::ENDED) return;
if(loaded > 0) return;
CCLOG("Started preloading!");
milliseconds startMiliseconds = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
for(int i = 0; i < files.size(); i++){
std::string filename = files.at(i) + "." + extension;
const int j = i;
cocos2d::experimental::AudioEngine::preload(filename, [&, filename, startMiliseconds, j](bool success){
CCLOG("%s done loading. Result: %s", filename.c_str(), success ? "success" : "failure");
loaded++;
if(loaded == files.size()){
CCLOG("All done!");
milliseconds endMiliseconds = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
milliseconds diff = endMiliseconds - startMiliseconds;
CCLOG("loading took: %lld ms", diff.count());
}
});
}
}
Loading took prints about 91ms on my device.
But let’s add one more thing here:
void HelloWorld::preloadSounds(Ref* ref, ui::Widget::TouchEventType touchEventType) {
if(touchEventType != ui::Widget::TouchEventType::ENDED) return;
if(loaded > 0) return;
CCLOG("Started preloading!");
milliseconds startMiliseconds = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
for(int i = 0; i < files.size(); i++){
std::string filename = files.at(i) + "." + extension;
const int j = i;
cocos2d::experimental::AudioEngine::preload(filename, [&, filename, startMiliseconds, j](bool success){
CCLOG("%s done loading. Result: %s", filename.c_str(), success ? "success" : "failure");
loaded++;
if(loaded == files.size()){
CCLOG("All done!");
milliseconds endMiliseconds = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
milliseconds diff = endMiliseconds - startMiliseconds;
CCLOG("loading took: %lld ms", diff.count());
}
});
if(i == 0){
cocos2d::experimental::AudioEngine::play2d(filename);
}
}
}
First sound is played before it loaded.
And then game freezes for a while and loading took prints 2143ms!
That time shouldn’t be that long, it should be more or less the same! It should play this sound after loading.
That wasn’t working like that while using SimpleAudioEngine. It took longer to load, but game wasn’t freezing.
I play tons of sounds in tons of screens in different situations and it’s hard to find all the ones which are played on the beginning - leave one and game is freezing hard.
Sorry about that while loop, it was confusing.