void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { // If there is already a background music source we stop it first if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { preloadBackgroundMusic(fullPath.c_str()); it = s_backgroundMusics.find(fullPath); } if (it != s_backgroundMusics.end()) { s_backgroundSource = it->second->source; alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); setBackgroundVolume(s_volume); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic:alSourcePlay"); } }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { preloadBackgroundMusic(fullPath.c_str()); it = s_backgroundMusics.find(fullPath); } if (it != s_backgroundMusics.end()) { s_backgroundSource = it->second->source; alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic"); } }
// // background audio // void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { ALuint buffer = AL_NONE; if (isOGGFile(fullPath.data())) { buffer = createBufferFromOGG(fullPath.data()); } else { buffer = alutCreateBufferFromFile(fullPath.data()); } checkALError("preloadBackgroundMusic"); if (buffer == AL_NONE) { fprintf(stderr, "Error loading file: '%s'\n", fullPath.data()); alDeleteBuffers(1, &buffer); return; } ALuint source = AL_NONE; alGenSources(1, &source); checkALError("preloadBackgroundMusic"); alSourcei(source, AL_BUFFER, buffer); checkALError("preloadBackgroundMusic"); backgroundMusicData* data = new backgroundMusicData(); data->buffer = buffer; data->source = source; s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data)); } }
// // background audio // void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { ALuint buffer = AL_NONE; bool success = false; OpenALFile file; file.debugName = pszFilePath; file.file = fopen(fullPath.c_str(), "rb"); if (!file.file) { fprintf(stderr, "Cannot read file: '%s'\n", fullPath.data()); return; } const std::vector<OpenALDecoder *> &decoders = OpenALDecoder::getDecoders(); for (size_t i = 0, n = decoders.size(); !success && i < n; ++i) success = decoders[i]->decode(file, buffer); file.clear(); ALuint source = AL_NONE; alGenSources(1, &source); checkALError("preloadBackgroundMusic:alGenSources"); alSourcei(source, AL_BUFFER, buffer); checkALError("preloadBackgroundMusic:alSourcei"); backgroundMusicData* data = new backgroundMusicData(); data->buffer = buffer; data->source = source; s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data)); } }