I have the following code that should mute audio when window is focused, but I must have done something wrong since it doesn't mute at all;
script
$(function() {
$(window).focus(function() {
console.log('Focus');
/*getElementById('notificationsound').muted = true;*/
$("#notificationsound").prop('muted', true); //mute
});
$(window).blur(function() {
console.log('Blur');
});
});
html
<audio id="notificationsound" src="sound/notify.mp3" preload="auto"></audio>
The goal is for the notification sound to be played only when the page is not viewed / out of focus (I'm using this "script structure" since it's likely I'll add more to the different states later on).
I know I'm missing something very basic since I'm a beginner, and since it logs focus/blur in the console correctly. Also – I know this most likely is a very similar question to others asked here before, but I could only find rather aged answers (which my code is based upon) so if this is not the "modern" or best practice anymore I'd love examples of alternate methods.
Examples are very much appreciated in case of major change is needed or suggested.
Aucun commentaire:
Enregistrer un commentaire