I have a site that will make use of the Soundcloud API, to play different tracks.
Perhaps I have some issues with switching between tracks and play/pause the active track. I think this has to do with the trackID and the first if statement of SCPlayer, because it will be true after you clicked the first track and it will be forever be true (only if you refresh it will be reset naturally).
My code looks like this:
var SCPlayer;
var isPlaying = false;
function player(trackID) {
console.log(trackID);
if (SCPlayer) {
if (isPlaying) {
SCPlayer.pause();
isPlaying = false;
} else {
SCPlayer.play();
isPlaying = true;
}
} else {
// SC.stream('/tracks/' + trackID, function(sound) {
SC.stream('/tracks/' + trackID).then(function(player) {
SCPlayer = player;
SCPlayer.play();
isPlaying = true;
SCPlayer = false;
});
}
}
$('.control').click(function(event) {
event.preventDefault();
var trackID = $(this).closest('li').attr('data-id');
// console.log(trackID);
if ( $(this).hasClass('play') ) {
$('#playlist li a').removeClass('pause');
$(this).removeClass('play').addClass('pause');
} else {
$('#playlist li a').removeClass('pause');
$(this).addClass('play');
}
player(trackID);
});
The above code will play the selected track. When you select another track it will play the another song. But I can't pause / play the selected track. When I remove SCPlayer = false;
the play/pause functionality will work and switching between tracks not, because what I mentioned before, it will hold the latest trackID.
Does anyone know a working solution for this? So that you can play/ pause the track and switch between tracks.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire