简体   繁体   中英

JavaScript Sounds on ClickEvent works only on google chrome

I have a problem with play sound on ie,ff and safari. Only chrome works. I want to play sounds when click in canvas. I have two sounds for it. But the sounds are only working on google chrome.

index.php

<div style="display:none">
        <audio id="soundTrue" class="soundTrue" controls preload="none"> 
            <source src="fx/true.ogg" type="audio/ogg">
            <source src="fx/true.wav" type="audio/wav">
            <source src="fx/true.mp3" type="audio/mpeg">
        </audio>
        <audio id="soundFalse" class="soundFalse" controls preload="none"> 
            <source src="fx/false.ogg" type="audio/ogg">
            <source src="fx/false.wav" type="audio/wav">
            <source src="fx/false.mp3" type="audio/mpeg">
        </audio>
    </div>

game.js:

var playSound = function (sound) {
    sound.trigger('play');
}

var stopSound = function (sound) {
    sound.trigger('pause');
    sound.prop("currentTime", 0);
}

$('#gameCanvas').click(function (e) {
document.getElementById('soundTrue').play();

var clickedX = e.pageX - this.offsetLeft;
var clickedY = e.pageY - this.offsetTop;
var found = false;

for (var i = 0; i < circles.length; i++) {
    if (clickedX < circles[i].right && clickedX > circles[i].left && clickedY > circles[i].top && clickedY < circles[i].bottom) {
        if (coordinates[i][2] === 0) {
            coordinates[i][2] = 1;
            drawFoundCupcake(coordinates[i][0], coordinates[i][1]);
            stopSound(soundTrue);
            playSound(soundTrue);

            countOfFound++;
       }
        found = true;
    }
}

if (!found) {
    stopSound(soundTrue);
    playSound(soundFalse);
} else{
    $('#countCupcakes').val(countOfFound);
}
});

This works in chrome and firefox but not in ie and safari. Any ideas?

var soundTrue = document.createElement('audio');
soundTrue.setAttribute('src', '/fx/true.mp3');
soundTrue.setAttribute('autoplay:false', 'autoplay');

var soundFalse = document.createElement('audio');
soundFalse.setAttribute('src', '/fx/false.mp3');
soundFalse.setAttribute('autoplay:false', 'autoplay');

var playSound = function (sound) {
    sound.play();
}

var stopSound = function (sound) {
    sound.pause();
    sound.currentTime = 0;
}

$('#gameCanvas').click(function (e) {
    var clickedX = e.pageX - this.offsetLeft;
    var clickedY = e.pageY - this.offsetTop;
    var found = false;

    for (var i = 0; i < circles.length; i++) {
        if (clickedX < circles[i].right && clickedX > circles[i].left && clickedY > circles[i].top && clickedY < circles[i].bottom) {
            if (coordinates[i][2] === 0) {
                coordinates[i][2] = 1;
                drawFoundCupcake(coordinates[i][0], coordinates[i][1]);
                stopSound(soundTrue);
                stopSound(soundFalse);
                playSound(soundTrue);

                countOfFound++;
            }
            found = true;
        }
    }

    if (!found) {
        stopSound(soundTrue);
        stopSound(soundFalse);
        playSound(soundFalse);
    } else {
        $('#countCupcakes').val(countOfFound);
    }

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