Background music with .mp3 file plays late

I use cocos2d-x RC1 and I need a background music to play from an mp3 file. The problem is that it starts to play after 2-3 seconds. I have tried to preload it but here is the problem:

void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
}

SimpleAudioEngine::preloadBackgroundMusic is a dummy function and does nothing. Also there is no callback that tells that background music is loaded, so that I could show loading screen untill that moment. Now I don’t know how to control the time the music will start to play.

Another was is the .wav file that plays immediately, but .wav file size is 49Mb, which is nonsense. Thus, I stuck. Please help.

I am not seeing any delay. I have an AppDirector class where I do some special things.

Consider this…

void AppDirector::createAudioEngine()
{
    CocosDenshion::SimpleAudioEngine* ae = CocosDenshion::SimpleAudioEngine::getInstance();
    ae->preloadBackgroundMusic("Tallahassee.mp3");
    ae->preloadBackgroundMusic("bigcountry.mp3");
    ae->preloadEffect("click.mp3");
    //ae->preloadEffect("tap.mp3");
    ae->setBackgroundMusicVolume(0.5f);
    ae->setEffectsVolume(0.3f);
    setAudioEngine(ae);
}

Then when I need to play music, I pass in the mp3 name as a string to:

void Utils::playMusic(std::string inMusicFileName, bool inRepeat)
{
        APPDIRECTORINSTANCE->getAudioEngine()->playBackgroundMusic(inMusicFileName.c_str(), inRepeat);
}

preloadBackgroundMusic does nothing right? Did you see the function definition?

Also how big your .mp3 file sizes are?

I see this:

void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
    // Changing file path to full path
    std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
    static_preloadBackgroundMusic(fullPath.c_str());
}

void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
    // Changing file path to full path
    std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
    static_playBackgroundMusic(fullPath.c_str(), bLoop);
}

3 or so MB each or less… But I was using 11+ mb mp3 just fine.

I use cocos2d-x 3.0 RC1, maybe You use RC2? Which one you use? Because in my case here is the function:

void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
}

That path to SimpleAudioEngine file is …cocos2d\cocos\audio\win32\SimpleAudioEngine.cpp

Yes, RC2 and at some point today the final release.

Please look here:

https://github.com/cocos2d/cocos2d-x/blob/develop/cocos/audio/win32/SimpleAudioEngine.cpp

preloadBackgroundMusic is empty. Please give me the path of your file where you have

void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
    // Changing file path to full path
    std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
    static_playBackgroundMusic(fullPath.c_str(), bLoop);
}

You develop your code on linux or mac?

oh, Mac. So why not back up your version and then paste in just the lines in each of those 2 functions and see if it works for you? If it does, file a bug. I dont have a Windows instance quite handy to test it for you.

You can not just copy and paste the code. The logic is completely different for different platforms. I need to file a bug I guess.

To be clear, I didn’t say copy in the whole .cpp, just the one function. Filing a bug is needed in any case.

I guess on Win32 they dont have the same ‘static_’ functions that the Mac is calling.

I didn’t meant the whole file too. :slight_smile: And you are right, there is no ‘static_’ functions on WIndows that the Mac is calling. Bug is filed: http://www.cocos2d-x.org/issues/4932

There is some solution http://blog.csdn.net/dinghqalex/article/details/41155699 . You can use new AudioEngine instead SimpleAudioEngine. But it awailable in cocos2dx.3.3.It works right.

Well I’m using a melange of both AudioEngine and SimpleAudioEngine, and it’s very fast and simple in the same time…require cocosd-x 3.3+… and it’s only for windows, the android code will be replaced automatically during the compilation process.
SimpleAudioEngine.zip (4.6 KB)