website/public/js/music.js
2016-11-28 16:36:14 -05:00

38 lines
931 B
JavaScript
Executable File

var player = '';
$(document).ready(function() {
player = $('#music-player').get(0);
$('.now-playing-list .music-track').on('click', function() {
// change the audio source
changeAudioSources(this);
});
});
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
playerSources.each(function() {
// jQuery-ize the element
source = $(this);
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();
// start playing new song
player.play();
}