Reputation: 1
I'm working on a project and in a case I need to play audio using tag in web. But I found out that the audio won't stop after lock screen or press home button. It just keeps playing. So is there a way to disable that? BTW I tried simplest way but the audio won't stop. Here's the code:
<html>
<body>
<audio src="./movies/1.mp3" controls="controls">
Your browser does not support the audio element.
</audio>
</body>
</html>
Maybe. I can listen to some event like hitting home button or lock screen and pause()? But I don't know if that possible.
Upvotes: 0
Views: 1540
Reputation: 155
Make use of a loop, to check if the user is on the webpage. Store the time.
Here is my code with jQuery:
var lastSeen
var loop = function (){
lastSeen = Date.now()
setTimeout(loop, 50)
}
loop()
var media = $('audio, video')
media.each(function () {
this.addEventListener('timeupdate', function () {
if(Date.now() - lastSeen > 100) {
this.pause()
}
}, false)
})
source: Stop HTML5 audio from looping when iOS Safari is closed
Upvotes: 1