Vibhav Aggarwal
Vibhav Aggarwal

Reputation: 3

Audio is unable to play via javascript on some mobile browsers

I'm trying to do a simple thing. I want some audio to play exactly after 10 seconds when the user enters the webpage. I used the following code

var aud=new Audio("someAudio.mp3");
$(document).ready(function(){
setTimeout(function(){aud.play()}, 10000);
});

It is working perfectly fine on desktop browsers. However, the audio is not playing in some mobile browsers like Google Chrome though it is working in Firefox. What may be the possible reason for this and how to fix it? I saw some similar questions but didn't find a suitable answer. Thanks in advance

Upvotes: 0

Views: 1580

Answers (1)

Brad
Brad

Reputation: 163468

I'm trying to do a simple thing. I want some audio to play exactly after 10 seconds when the user enters the webpage.

You can't unless there has been user interaction.

Handle a click event for some element. In that event handler, play some other audio. (This audio can be silent!) After 10 seconds have passed from load, if the user has touched/clicked something, and you've done this, you should be able to play your audio file.

Upvotes: 1

Related Questions