SoundManager.lua 4.49 KB
cc.exports.SoundManager = {}

soundKindArr = {"loli","ladylike"};

SoundManager.isBackgroundMusic = true;
SoundManager.isEffect = true;
SoundManager.isPlayingBossEffect = false;
--local audioEngine = cc.SimpleAudioEngine:getInstance();

SoundManager.cur_playing_music_path = "";

local function init()
    if cc.UserDefault:getInstance():getStringForKey("isMusic") == "" then
        cc.UserDefault:getInstance():setStringForKey("isMusic",1);
    end
    if cc.UserDefault:getInstance():getStringForKey("isEffect") == "" then
        cc.UserDefault:getInstance():setStringForKey("isEffect",1);
    end
    if cc.UserDefault:getInstance():getStringForKey("isMusic") == "1" then
        SoundManager.isBackgroundMusic = true;
    else
        SoundManager.isBackgroundMusic = false;
    end
    if cc.UserDefault:getInstance():getStringForKey("isEffect") == "1" then
        SoundManager.isEffect = true;
    else
        SoundManager.isEffect = false;
    end
end
init();

function SoundManager.playMusic(filePath,loop)
    if filePath then
        SoundManager.cur_playing_music_path = filePath;
    end
    if SoundManager.isBackgroundMusic then
--        audioEngine:playMusic(filePath,loop);
        audio.playMusic(SoundManager.cur_playing_music_path,loop);
        SoundManager.setMusicVolume(1.5);
--        audioEngine:setMusicVolume(1);
    end
end

function SoundManager.stopMusic()
    if audio.isMusicPlaying() then
--        audioEngine:stopMusic(false);
        audio.stopMusic(true)
--        audioEngine:setMusicVolume(0);
    end
end

--暂停音乐
function SoundManager.pauseMusic()
    audio.pauseMusic()        
end

--继续音乐
function SoundManager.resumeMusic(filePath)
    if filePath then
        SoundManager.cur_playing_music_path = filePath;
    end
    if audio.isMusicPlaying() then
        audio.resumeMusic() 
    else
        SoundManager.playMusic(SoundManager.cur_playing_music_path,true)
    end       
end

function SoundManager.setMusicVolume(value)
    --if SoundManager.isMusicPlaying() then
    if audio.isMusicPlaying() then
        audio.setMusicVolume(value)
    end
--    audioEngine:setMusicVolume(value);
    --end
end

function SoundManager.playEffect(filePath,isLoop,volume)
    if isLoop==nil then
        isLoop = false
    end
    if SoundManager.isEffect then
        volume = volume or 1;
        SoundManager.setSoundsVolume(volume);
        return audio.playSound(filePath,isLoop)
    end
end

function SoundManager.setSoundsVolume(value)
    audio.setSoundsVolume(value);
end

function SoundManager.isMusicPlaying()
    return audio.isMusicPlaying();
end

function SoundManager.setIsBackgroundMusic(b)
    SoundManager.isBackgroundMusic = b;
    if b then
        cc.UserDefault:getInstance():setStringForKey("isMusic",1);
    else
        cc.UserDefault:getInstance():setStringForKey("isMusic",0);
    end
end

function SoundManager.setIsEffect(b)
    SoundManager.isEffect = b;
    if b then
        cc.UserDefault:getInstance():setStringForKey("isEffect",1);
    else
        cc.UserDefault:getInstance():setStringForKey("isEffect",0);
    end
end

function SoundManager.stopEffect(handle)
    audio.stopSound(handle)
end

function SoundManager.getGuandanSoundPath(name)
    return "res/sound/gd/"..name;
end

function SoundManager.getGuandanVoicePath(name)
    local kind = math.random(1,2);
    math.randomseed(tostring(os.time()+kind):reverse():sub(1, 6));
    return "res/sound/gd/"..soundKindArr[kind].."/"..name;
end

function SoundManager.getMJEffectPath(name,sex)
    if sex == 1 then
        return "res/sound/mj/sichuan/nan/"..name;
    else
        return "res/sound/mj/sichuan/nv/"..name;
    end
end

function SoundManager.getMJChatSoundPath(name,sex)
    if sex == 1 then
        return "res/sound/mj/putonghua/nan/"..name;
    else
        return "res/sound/mj/putonghua/nv/"..name;
    end
end

function SoundManager.getNiuniuChatSoundPath(name,sex)
    local path = ""
    if sex == 1 then
        path = "res/sound/nn/nan/"..name;
    else
        path = "res/sound/nn/nv/"..name;
    end
    print("play nn chat voice:",path);
    return path;
end

function SoundManager.getDdzEffectPath(name)
    return "res/sound/ddz/"..name;
end

function SoundManager.getDdzChatPath(name,sex)
    if sex == 1 then
        return "res/sound/ddz/boychat/"..name;
    else
        return "res/sound/ddz/girlchat/"..name;
    end
end

function SoundManager.getDdzPaixingPath(name,sex)
    if sex == 1 then
        return "res/sound/ddz/man/"..name;
    else
        return "res/sound/ddz/woman/"..name;
    end
end

return SoundManager