+ 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

22nd Jun 2020, 2:35 PM
Gaia
Gaia - avatar
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(); }
22nd Jun 2020, 3:20 PM
Abhay
Abhay - avatar
+ 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
22nd Jun 2020, 3:29 PM
Gaia
Gaia - avatar