Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11
yes execute play() event when a click is detected on body or your required element $(document).ready(function(){ // audio.click = audio.play() //blocked function load(){ document.body.addEventListener('click', function(){ audio.play() // alert('clicked, how?') }) // document.body.click() } load() })
15th Dec 2017, 1:57 AM
Lord Krishna
Lord Krishna - avatar
+ 10
I have looked for a solution still found none. i tried the below code, but autoplay is blocked. you will need the user to press some button for it to work. that's the solution i know so far. <button id='btn></button> <audio id='audio'></audio> $(document).ready(function(){ // audio.click = audio.play() function load(){ btn.onclick = function(){alert("clicked by me not you :P ") } btn.click()//simulate a click document.body.addEventListener('click', function(){ btn.onclick = audio.play() // alert('tap the button for a surprise') }) document.body.click() } load() }) check the below SO link may be you may find something of use. https://stackoverflow.com/questions/12206631/html5-audio-cant-play-through-javascript-unless-triggered-manually-once
15th Dec 2017, 1:16 AM
Lord Krishna
Lord Krishna - avatar
+ 7
Unfortunately play() method can only be initiated by user itself. It's added for safety but not good in cases like your code or game development (sounds in games on js). But there is the way to do this by adding eventListener like button or other to user to interact and run playing sound.
15th Dec 2017, 1:18 AM
K137(){/**/};
K137(){/**/}; - avatar