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
  • 23,637
  • 18
  • 82
  • 118
Varun Agarwal
  • 299
  • 3
  • 12
  • Look my answer here: http://stackoverflow.com/questions/11085409/detect-moving-to-a-new-tab-in-mobile-safari/11164387#11164387. It's to detect the focus! – Mageek Jun 24 '12 at 11:22
  • @Mageek I have to stop the audio when the application tab is hidden or minimised....please suggest something.?? – Varun Agarwal Jun 24 '12 at 11:51
  • "When the application tab is hidden or minimised" means onblur, and I don't know any way how to detect the "onblur" event :( If you find a way later, it would be good to post it here. – Mageek Jun 24 '12 at 12:36
  • @Mageek it is possible, check answer. – eric.itzhak Jun 24 '12 at 13:33

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
  • 31,604
  • 74
  • 198
  • 296
-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,252
  • 26
  • 85
  • 139