HSoundUtils.cpp
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
//  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);
}