Home jquery play stop audio file
Reply: 1

jquery play stop audio file

eskimopest
1#
eskimopest Published in 2018-02-12 12:14:08Z

I have a list of songs and the way im using to access them is :

<a href="path to song 1">play</a>
<a href="path to song 2">play</a>
...

when the link is clicked, the song starts and im doing this with jquery like this:

$('a').on('click', function() {
    var path = $(this).attr('href');
    var song = new Audio(path);
    song.play();
});

How can i make a play/stop toggle when the link is clicked?

Pedram
2#
Pedram Reply to 2018-02-12 12:58:22Z

Toggle play() and pause() functions like this:

var isPlaying = false
$("a").on("click", function(e) {
  e.preventDefault();

  var path = $(this).attr('href');
  var song = new Audio(path);

  if (!isPlaying) {
    isPlaying = true;
    song.play();
    $(this).text("Pause")
  } else {
    isPlaying = false;
    song.pause();
    $(this).text("Play");
  }

  console.log(isPlaying);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<a href="path to song 1">play</a>
<a href="path to song 2">play</a>

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299088 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO