D
D
DivineDraft2017-01-21 14:17:02
HTML
DivineDraft, 2017-01-21 14:17:02

How to automatically leave the previous song playing when listening to a new one?

I want to display several audio players on one page, I output each song like this:

<audio id="sound" src="http://dump.bitcheese.net/files/wikamax/shik.mp3" preload="none" controls></audio>

And so that when playing a new track, the old one stops automatically.
As I understand it, you need to use JS?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2017-01-21
@DivineDraft

jQuery

<script>
$(function() {
    $('audio').on('play', function() {
    $('audio').addClass('stoped').removeClass('playing');
        $(this).removeClass('stoped').addClass('playing');
        $('.stoped').each(function() {
            $(this).trigger('pause');
            $(this)[0].currentTime = 0;
        })
    })
})
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question