简体   繁体   中英

chat sound not played in javascript

I am facing a problem for playing sound in javascript. The alert box is displayed but the chat sound doesn't play. Following is the code of chat.js.

for (x in newMessages) {
    if (newMessages[x] == true) {

    if (chatboxFocus[x] == false) {
        //FIXME: add toggle all or none policy, otherwise it looks funny
        $('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
        alert('before ');
            var snd = new Audio("facebookchat.mp3");  
             snd.play();
        alert('middle');

        alert('after');
    }
}

}

Try this:

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'facebookchat.mp3');
audioElement.load()
audioElement.addEventListener("load", function() { 
  audioElement.play(); 
}, true);

Reffered: How to use a javascript to autoplay a html5 audio tag

It might help, in your case.

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