+ 1
.play() is not working!
When I try to make a Breakout game, I want to make code that makes, the background music will light up when the ball destroys a block. But when i try to make it, there's an error in console, the error is .play() can only be initiated by a user gesture
3 Réponses
+ 2
Try this
function audio(source){
var playaudio=document.createElement("audio");
playaudio.controls="none";
playaudio.preload="auto";
playaudio.src=source;
this.playing=function(){
playaudio.play();
}
this.pausing=function(){
playaudio.pause();
}
}
var play1=new audio("audio link");
if(some condition){
play1.playing();
}
+ 1
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.sound.play();
}
new sound("url");
I use this