简体   繁体   中英

SoundManager2 Playing Multiple Sounds at once on Android

If you have two sounds in soundmanager2 normally you can play both at the same time, on android and other mobiles calling play on one sound will stop all other sounds.

soundManager.setup({
    preferFlash: false,
    //, url: "swf/"
    onready: function () {
        soundManager.createSound({
            url: [
                "http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3", "http://www.html5rocks.com/en/tutorials/audio/quick/test.ogg"
            ],
            id: "music"
        });
        soundManager.createSound({
            url: [
                "http://www.w3schools.com/html/horse.mp3", "http://www.w3schools.com/html/horse.ogg"
            ],
            id: "horse"
        });
    }
}).beginDelayedInit();

$("#horse").click(function () {
    soundManager.play("horse"); // <- stops other sounds on mobile :(
});

$("#music").click(function() {
    soundManager.play("music"); // <- stops other sounds on mobile :(
});

http://jsbin.com/hopapagefa/1/edit?html,js,output

Is there some way i can run multiple sounds on android? If not is there some way i can detect this ability to only run one sound at once?

reasoning: I want to have constant background music in a game and play a sound when something happens ideally without stopping the background music.

I didn't find a way to run multiple sounds on android but what i did was detect if sounds had been stopped by this limitation and the restart them where they were once the sound had finished playing eg

var isPlaying = function (name) {
    return soundManager.getSoundById(name).playState == 1;
};

// we want to play horse music over our background music
var oldPosition = soundManager.getSoundById('music').position;
soundManager.play("horse", {
    onfinish: function() {
        if(!isPlaying('music')) { 
            // mobile devices must have cut the background music off :(

            // should probably check if its still appropriate to replay the background music
            soundManager.play("music", {
                position: oldPosition
            });
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM