user3378335
user3378335

Reputation: 21

How to stop playing music when the user leaves the page

How to stop playing music when the user leaves the page with SoundCloud container?

I get this definition of stopAll :

 stopAll = function() {
      $('.sc-player.playing a.sc-pause').click();
 };

But I do not know how to use it Please help me to add this.

Upvotes: 2

Views: 483

Answers (1)

chengmu
chengmu

Reputation: 21

I take it as you want to silence the music when user switch to another tab?

You can try this API: Page visibility, something like this:

//startSimulation and pauseSimulation defined elsewhere
function handleVisibilityChange() {
  if (document.hidden) {
    stopAll();
  } 
}

document.addEventListener("visibilitychange", handleVisibilityChange, false);

but this api gets some compatibility issues, it wont work on ie version below 10.

I used to apply a method which I don't feel very comfortable ... Use document's blur event. It can work, but not very nice I think.

If you mean user closes the tab, then it will be the onunload event; but I don't think you would need to silence the music if they already closed the whole page....

hope my answer helps :)

Upvotes: 1

Related Questions