title: How can I tell if an HTML5 Audio element is playing with Javascript
body: <p>I have an <code>audio</code> element in a webpage, and I want to ensure that a user is not still playing it when they leave the page. <strong>How can I be sure the <code>audio</code> element is not playing when the page is unloaded?</strong> So far, I have the following code, but it doesn't seem to work; the dialog that pops up upon unload reports that <code>playing</code> is <code>false</code> even when the audio is playing:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;&lt;html&gt;
  &lt;head&gt;
      &lt;script&gt;&lt;!-- Loading Scripts --&gt;
      function unloadTasks(){
          if (playing &amp;&amp; !window.confirm("A podcast is playing, and navigating away from this page will stop that. Are you sure you want to go?"))
              window.alert("Here is where I will stop the page from unloading... somehow");
      }
      &lt;/script&gt;
      &lt;script&gt;&lt;!-- Player Scripts --&gt;
      var playing = false;
      function logPlay(){
          playing = isPlaying("e1audPlayer");
      }
      function isPlaying(player){
          return document.getElementById(player).currentTime &gt; 0 &amp;&amp; !document.getElementById(player).paused &amp;&amp;  !document.getElementById(player).ended;
      }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onunload="unloadTasks()"&gt;
  &lt;audio id="e1audPlayer" style="width:100%;" controls="controls" preload="auto" onplaying="logPlay()" onpause="logPlay()"&gt;
      &lt;source src="http://s.supuhstar.operaunite.com/s/content/pod/ADHD Episode 001.mp3" type="audio/mpeg"/&gt;
      &lt;source src="http://s.supuhstar.operaunite.com/s/content/pod/ADHD Episode 001.ogg" type="audio/ogg"/&gt;
      Your browser does not support embedded audio players. Try &lt;a href="http://opera.com/"&gt;Opera&lt;/a&gt; or &lt;a href="http://chrome.com/"&gt;Chrome&lt;/a&gt;
  &lt;/audio&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><a href="http://s.supuhstar.operaunite.com/s/content/pod/playerTest.htm" rel="noreferrer">Working example</a></p>