getting audio element to play after a pause not working in jquery -


i trying audio element able played , paused many times user hits button. far can audio play after first push turns icon pause. if user clicks again pause audio , turn icon play button. after cannot work anymore.

html

<a href="'.$row['audio'].'" target="_blank"><i class="fa fa-cloud-download"></i></a> <a href="#" class="fa fa-play button-play"><audio src="'.$row['audio'].'" id="mytune1" class="mytune1" preload="none"></audio></a> 

jquery

$(document).on("click", ".button-play", function() {      var audioelement = $(this).find('.mytune1')[0];     $(this).blur();     $(this).addclass("active");     $(this).removeclass("active");     $(this).replacewith('<a href="#" class="fa fa-pause button-pause"><audio src="'+audioelement+'" id="mytune1" class="mytune1" preload="none"></audio></a>');     audioelement.play();     thisaudioelement = audioelement; }); $(document).on("click", ".button-pause", function() {      var audioelement = $(this).find('.mytune1')[0];     $(this).blur();     $(this).addclass("active");     $(this).removeclass("active");     $(this).replacewith('<a href="#" class="fa fa-play button-play"><audio src="'+audioelement+'" id="mytune1" class="mytune1" preload="none"></audio></a>');     thisaudioelement.pause(); }); 

audioelement is object, still need retrieve it's src property value it:

....src="'+ audioelement.src +'" id="mytune1"...

also there's no absolute need stuff like

$(this).addclass("active");    // ok.... added but... $(this).removeclass("active"); // what's 

ok, how than?

believe or not code you'll possibly need:

$(document).on("click", ".button-play", function( evt ) {    evt.preventdefault(); // prevents page scroll top (since you're using <a> anchor)    var audio = $(this).find('audio')[0];    audio[audio.paused?"play":"pause"]();    $(this).toggleclass("fa-pause");      // toggle pause icon  });
.button-play{    display:inline-block;    padding:8px 10px;    line-height:38px;    text-align:center;    text-decoration:none;    color:#fff;    background: lime;    box-shadow: 0 1px 3px rgba(0,0,0,0.3);    border-radius: 50%;  }  .button-play.fa-pause{    background: tomato;  }
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>      <a href="#" class="fa fa-play button-play">    <audio loop src="http://upload.wikimedia.org/wikipedia/en/4/45/acdc_-_back_in_black-sample.ogg" preload="none"></audio>  </a>

p.s: future, when see similar chunks of code provided one, it's clear sign same can improved , minimized essential.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -