HSoundUtils.cpp 2.71 KB
//
//  HSoundUtils.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 10.05.2017.
//
//

#include "HSoundUtils.h"
#include "HMathUtils.h"
#include "audio/include/AudioEngine.h"

int HSoundUtils::currentMusicSoundId = cocos2d::AudioEngine::INVALID_AUDIO_ID;
std::string HSoundUtils::currentMusicFilePath = "";


    unsigned int HSoundUtils::playRandomSound(std::string soundFolderPath, std::vector< std::string> soundNames, bool stopEffects){
        auto soundsLength = soundNames.size();
        if(soundsLength > 0){
            int randomIndex = HMathUtils::getRandomInt(0, (int)soundsLength-1);
            std::string fullSoundPath = soundFolderPath == "" ? soundNames[randomIndex] : soundFolderPath + "/" + soundNames[randomIndex];
            return playSound(fullSoundPath, stopEffects);
        }
    
        return 0;
    }


void HSoundUtils::stopAllSounds(bool musicToo){
    if(musicToo){
        cocos2d::AudioEngine::stopAll();
    } else {
        cocos2d::AudioEngine::stopAllBut(currentMusicSoundId);
    }
}

void HSoundUtils::stopSoundById(int audioId){
    cocos2d::AudioEngine::stop(audioId);
}

int HSoundUtils::playSound(const std::string soundFilepath, bool stopOtherEffects){
    if(stopOtherEffects){
//        cocos2d::AudioEngine::stopAll();
        cocos2d::AudioEngine::stopAllBut(currentMusicSoundId);
    }
    auto soundId = cocos2d::AudioEngine::play2d(soundFilepath);
//    cocos2d::AudioEngine::setVolume(soundId, SOUND_EFFECTS_VOLUME);
    return soundId;
//return 0;
}

int HSoundUtils::playMusic(const std::string musicFilepath){
//    CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(MUSIC_VOLUME_BASE);
//    CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(musicFilepath.c_str(), true);
    
    if(currentMusicFilePath != musicFilepath){
        stopSoundById(currentMusicSoundId);
        currentMusicSoundId = cocos2d::AudioEngine::play2d(musicFilepath, true);
        currentMusicFilePath = musicFilepath;
    }
//    cocos2d::AudioEngine::setVolume(currentMusicSoundId, MUSIC_VOLUME_BASE);
    return currentMusicSoundId;
}

void HSoundUtils::stopMusic(){
    cocos2d::AudioEngine::stop(currentMusicSoundId);
    currentMusicSoundId = cocos2d::AudioEngine::INVALID_AUDIO_ID;
    currentMusicFilePath = "";
}

void HSoundUtils::pauseMusic(){
    cocos2d::AudioEngine::pause(currentMusicSoundId);
}

void HSoundUtils::resumeMusic(){
    cocos2d::AudioEngine::resume(currentMusicSoundId);
}

void HSoundUtils::pauseAllSounds(){
    cocos2d::AudioEngine::pauseAll();
}

void HSoundUtils::resumeAllSounds(){
    cocos2d::AudioEngine::resumeAll();
}

void HSoundUtils::preloadSoundEffect(std::string soundPath){
    cocos2d::AudioEngine::preload(soundPath);
}