2

I am playing audio in Background of an application but I want to stop when user minimises or Safari goes to background.

I have already tried working element.onfocus.

I also tried to use window.requestAnimFrame but its results are not satisfactory.

I can't figure out how to stop the audio.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Varun Agarwal
  • 299
  • 3
  • 12

2 Answers2

1

you might have success with the pageshow and pagehide events, which are triggered when safari goes to the background.

however there is no event that i know of, when you switch tabs within safari.

also here is a small library that does most of the work for you: https://github.com/zynga/jukebox

clamp
  • 33,000
  • 75
  • 203
  • 299
-1

As answer to you and to Mageek comment, you should check the onfocus event and the onblur event (it is possible) prefered using jQuery :

$(window).focus(function() { 
//enable sound
}); 

$(window).blur(function() { 
//disable sound
}); 

This will probably do the trick, but havn't tested it.

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142