Music not playing

Here’s my code:

auto fileUtils = FileUtils::getInstance();
std::string musicFile = fileUtils->getWritablePath() + “music/music.wav”;
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(musicFile.c_str(), true);

No error is reported but I hear no sound. What am I missing? This is on iOS Simulator.

Thanks!
– Sumeet

There are 2 things you should do.

  1. Convert you audio to caf - I’m not 100% sure if wav is supported by iOS naively, But it is recommended to just convert the audio to caf for iOS (OGG for Android if your building for that as well)

  2. use SetSearchPaths - basically telling cocos2d::FileUtils Where to search for you content. In your case, your code would change to comthing like this:

    std::vectorstd::string search_paths;

    // Setting search paths for music directory.
    search_paths.push_back(“music”);

    // Set |search_paths| to FileUtils.
    cocos2d::FileUtils::getInstance()->setSearchPaths(search_paths);

    // Play sound.
    CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(“music.caf”, true);

Thanks @mozartalouis!

I actually tried setting search paths and that gave me a “File not found” error. And I tried mp3 format as well. I’ll try .caf next.

Thanks a lot!

guys, I keep getting the file not found error. I did add the music dir to search paths. I’m doing far more complicated stuff with AssetsManagerEx and no problem finding downloaded resources. Here’s my dir layout:

Resoures
|-music
|–music.caf

code:

FileUtils::getInstance()->addSearchPath("music");
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("music.caf", true);

error:

cocos2d: fullPathForFilename: No file found at music.caf. Possible missing file.

super bummed! :frowning:

I didn’t add the Resource/music folder to the project target <-- stupid man

beautiful music working now!!

thanks for reading!