audiojs
audiojs copied to clipboard
Play one audio element at a time for AJAX added elements
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?
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 No I did not find a solution to this issue.
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!
Same issue. Anyone find a solution?