website/public/js/music.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-11-28 16:11:04 -05:00
var player = '';
2015-10-06 12:36:47 -04:00
$(document).ready(function() {
2016-11-28 16:36:14 -05:00
player = $('#music-player').get(0);
2016-11-28 16:11:04 -05:00
2016-11-28 16:36:14 -05:00
$('.now-playing-list .music-track').on('click', function() {
// change selected track
changeSelectedTrack(this);
2016-11-28 16:11:04 -05:00
});
2016-11-28 11:17:28 -05:00
});
2016-11-28 16:11:04 -05:00
function changeSelectedTrack(trackElem) {
var oldSelected = $('.now-playing-list .music-track.selected');
var newSelected = $(trackElem);
if (oldSelected.data('trackid') != newSelected.data('trackid')) {
// remove selected class from the old element and add it to the new one
oldSelected.removeClass('selected');
newSelected.addClass('selected');
// now change the audio sources
changeAudioSources(trackElem);
}
}
2016-11-28 16:11:04 -05:00
function changeAudioSources(trackElem) {
trackElem = $(trackElem);
var playerSources = $('#music-player source');
// retrieve audio file path from selected element
var audioFilePath = trackElem.data('path');
// loop through the audio player source elements
2016-11-28 16:36:14 -05:00
playerSources.each(function() {
// jQuery-ize the element
2016-11-28 16:36:14 -05:00
source = $(this);
2016-11-28 16:11:04 -05:00
if (source.attr('type') == 'audio/ogg') {
// OGG source file
source.attr('src', audioFilePath + '.ogg');
} else if (source.attr('type') == 'audio/mpeg') {
// MP3 source file
source.attr('src', audioFilePath + '.mp3');
}
});
// reload music player
player.load();
2016-11-28 16:36:14 -05:00
// start playing new song
player.play();
2016-11-28 16:11:04 -05:00
}