audiojs icon indicating copy to clipboard operation
audiojs copied to clipboard

Play one audio element at a time for AJAX added elements

Open jaskaran27 opened this issue 10 years ago • 4 comments

This is my script to attach the audio.js player to the <audio> elements and to play only one <audio> element at a time.

<script>
    audiojs.events.ready(function() {
        var aj = audiojs.createAll();
        $('.audiojs .play-pause').on('click', function(){
            var thisIndex = $(this).parents('.audiojs').index('.audiojs');
            $.each(aj, function(index,val){
                if ( index != thisIndex && aj[index].playing ) aj[index].pause(); 
            });
        });
    });
</script>

This works fine for the initially loaded elements.

If I attach this script in the AJAX template, the audio.js player is attached to the <audio> elements but none of them play, even the initially loaded <audio> elements don't play.

<script type="text/javascript">
    $(document).ready(function() {
        $('#loadmore-dj').on('click', function() {
            $('#loadmore-dj').hide();
            $('#loadmore-dj-gif').show();
            $.ajax({
                type: "GET",
                url: "/loadmore/dj/",
                data: {
                    'slug': $('.dj_slug').text().trim(),
                    'song_rank': $("#dj_song_list").find('.song_block').length
                },
            }).done(function (response) {
                $('#dj_song_list').append(response);
                audiojs.events.ready(function() {
                    var as = audiojs.createAll();
                    $('body').on('click', '.audiojs .play-pause', function(){
                        var thisIndex = $(this).parents('.audiojs').index('.audiojs');
                        $.each(as, function(index,val){
                            if ( index != thisIndex && as[index].playing ) as[index].pause(); 
                        });
                    });
                });
                $('#loadmore-dj').show();
                $('#loadmore-dj-gif').hide();
            }).done(hideLoadMore);
        });
    });
</script>

If I just attach the basic audio.js script(shown below) to the AJAX template to initialise the <audio> elements the <audio> elements work but playing the one <audio> element at a time part doesn't work.

audiojs.events.ready(function() {
    var as = audiojs.createAll();
});

How can I attach the audio.js player to all my <audio> element and play only one of them at a time?

jaskaran27 avatar Jul 23 '14 16:07 jaskaran27

hi @jaskaran27 I'm facing the same issue that as you explained above. Did you found any solution for that issue ? Will please paste your code if yes ?

srinu4122 avatar Sep 02 '14 05:09 srinu4122

@srinu4122 No I did not find a solution to this issue.

jaskaran27 avatar Sep 03 '14 04:09 jaskaran27

hi jaskaran27,

I borrowed your solution for playing one audio at a time and trying to also reset/rewind the paused audio file but nothing I've tried seems to work. is there something simple I'm missing?

thanks!

0Bloodymary0 avatar Sep 28 '14 16:09 0Bloodymary0

Same issue. Anyone find a solution?

jtphelan avatar Jul 20 '15 16:07 jtphelan