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-29 00:04:40 -05:00
|
|
|
// set the player's volume to 0.5 by default
|
|
|
|
player.volume = 0.5;
|
|
|
|
|
2016-11-28 20:13:09 -05:00
|
|
|
// on music track click
|
2016-11-29 00:02:29 -05:00
|
|
|
$('.now-playing-list').on('click', '.music-track', function() {
|
2016-11-28 16:51:29 -05:00
|
|
|
// change selected track
|
|
|
|
changeSelectedTrack(this);
|
2016-11-28 16:11:04 -05:00
|
|
|
});
|
2016-11-28 20:13:09 -05:00
|
|
|
|
|
|
|
// on album art click
|
2016-11-29 00:01:42 -05:00
|
|
|
$('.music-album').on('click', '.thumbnail', function() {
|
2016-11-28 20:13:09 -05:00
|
|
|
// change selected album
|
|
|
|
changeSelectedAlbum(this);
|
|
|
|
});
|
2016-11-29 12:18:25 -05:00
|
|
|
|
|
|
|
// on track end
|
2016-11-29 12:20:04 -05:00
|
|
|
$(player).on('ended', function() {
|
2016-11-29 12:18:25 -05:00
|
|
|
// find selected track
|
2016-11-29 12:20:04 -05:00
|
|
|
changeSelectedTrack($('.music-track.selected').next().get(0));
|
2016-11-29 12:18:25 -05:00
|
|
|
});
|
2016-11-28 11:17:28 -05:00
|
|
|
});
|
2016-11-28 16:11:04 -05:00
|
|
|
|
2016-11-28 20:09:37 -05:00
|
|
|
function changeSelectedAlbum(albumElem) {
|
2016-11-28 20:39:13 -05:00
|
|
|
var oldSelected = $('.music-album.selected');
|
2016-11-28 20:16:54 -05:00
|
|
|
var newSelected = $(albumElem).parent();
|
2016-11-28 20:09:37 -05:00
|
|
|
|
|
|
|
if (oldSelected.data('albumid') != newSelected.data('albumid')) {
|
|
|
|
// remove selected class from the old element and add it to the new one
|
|
|
|
oldSelected.removeClass('selected');
|
|
|
|
newSelected.addClass('selected');
|
|
|
|
|
2016-11-28 23:27:10 -05:00
|
|
|
// change album info
|
2016-11-28 21:26:47 -05:00
|
|
|
getAlbumInfo(albumElem);
|
2016-11-28 23:27:10 -05:00
|
|
|
// change available songs
|
|
|
|
getAlbumSongs(albumElem);
|
2016-11-28 20:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 21:23:55 -05:00
|
|
|
function getAlbumInfo(albumElem) {
|
2016-11-28 21:32:02 -05:00
|
|
|
albumElem = $(albumElem).parent();
|
2016-11-28 21:23:55 -05:00
|
|
|
|
|
|
|
$.get('/apiv1/music/album-info/' + albumElem.data('albumid'), function(data) {
|
|
|
|
var json = $.parseJSON(data);
|
|
|
|
|
|
|
|
// change album artwork
|
|
|
|
$('#album-artwork').attr('src', json.album_art);
|
|
|
|
// change album title
|
2016-11-29 17:14:48 -05:00
|
|
|
$('#album-title').text(json.title);
|
2016-11-29 11:51:17 -05:00
|
|
|
// change album release date
|
2016-11-29 12:06:26 -05:00
|
|
|
$('#album-release span').text(formatReleaseDate(json.release_date));
|
2016-11-28 21:23:55 -05:00
|
|
|
// change album description
|
|
|
|
$('#album-description').text(json.description);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-29 12:06:26 -05:00
|
|
|
function formatReleaseDate(d) {
|
|
|
|
var monthNames = [
|
|
|
|
"January", "February", "March",
|
|
|
|
"April", "May", "June", "July",
|
|
|
|
"August", "September", "October",
|
|
|
|
"November", "December"
|
|
|
|
];
|
|
|
|
|
|
|
|
var date = new Date(d);
|
|
|
|
var day = date.getDate();
|
|
|
|
var monthIndex = date.getMonth();
|
|
|
|
var year = date.getFullYear();
|
|
|
|
|
|
|
|
return monthNames[monthIndex] + ' ' + day + ', ' + year;
|
|
|
|
}
|
|
|
|
|
2016-11-28 23:24:18 -05:00
|
|
|
function getAlbumSongs(albumElem) {
|
2016-11-28 23:28:04 -05:00
|
|
|
albumElem = $(albumElem).parent();
|
2016-11-28 23:24:18 -05:00
|
|
|
|
|
|
|
$.get('/apiv1/music/album-songs/' + albumElem.data('albumid'), function(data) {
|
|
|
|
var json = $.parseJSON(data);
|
2016-11-28 23:47:11 -05:00
|
|
|
var playlist = $('.now-playing-list');
|
2016-11-28 23:24:18 -05:00
|
|
|
playlist.empty();
|
|
|
|
|
|
|
|
for (var i = 0; i < json.songs.length; i++) {
|
|
|
|
var song = json.songs[i];
|
2016-11-28 23:47:11 -05:00
|
|
|
|
|
|
|
var songElem = $('<li></li>')
|
|
|
|
.addClass('music-track')
|
2016-11-29 00:01:42 -05:00
|
|
|
.attr('data-trackid', song.id)
|
|
|
|
.attr('data-title', song.title)
|
|
|
|
.attr('data-album', song.album_id)
|
|
|
|
.attr('data-order', song.track_order)
|
|
|
|
.attr('data-path', song.audio_file);
|
2016-11-28 23:24:18 -05:00
|
|
|
|
|
|
|
if (i === 0) {
|
2016-11-28 23:47:11 -05:00
|
|
|
songElem.addClass('selected');
|
2016-11-28 23:24:18 -05:00
|
|
|
}
|
|
|
|
|
2016-11-28 23:47:11 -05:00
|
|
|
songElem.html('<span>' + song.track_order + '. ' + song.title + '</span>');
|
2016-11-28 23:24:18 -05:00
|
|
|
|
2016-11-28 23:47:11 -05:00
|
|
|
playlist.append(songElem);
|
2016-11-28 23:24:18 -05:00
|
|
|
}
|
2016-11-28 23:47:11 -05:00
|
|
|
|
2016-11-28 23:51:33 -05:00
|
|
|
changeAudioSources(playlist.find('.selected'), false);
|
2016-11-28 23:24:18 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-28 16:51:29 -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
|
2016-11-28 23:51:33 -05:00
|
|
|
changeAudioSources(trackElem, true);
|
2016-11-28 16:51:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 23:51:33 -05:00
|
|
|
function changeAudioSources(trackElem, autoplay) {
|
2016-11-28 16:11:04 -05:00
|
|
|
trackElem = $(trackElem);
|
|
|
|
var playerSources = $('#music-player source');
|
|
|
|
|
2016-11-28 23:51:33 -05:00
|
|
|
if (autoplay === null) {
|
|
|
|
autoplay = false;
|
|
|
|
}
|
|
|
|
|
2016-11-28 16:11:04 -05:00
|
|
|
// 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() {
|
2016-11-28 16:12:30 -05:00
|
|
|
// jQuery-ize the element
|
2016-11-28 16:36:14 -05:00
|
|
|
source = $(this);
|
2016-11-28 16:12:30 -05:00
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-29 12:10:41 -05:00
|
|
|
// change track title
|
|
|
|
$('#track-title').text(trackElem.data('title'));
|
|
|
|
|
2016-11-28 16:11:04 -05:00
|
|
|
// reload music player
|
|
|
|
player.load();
|
2016-11-28 23:51:33 -05:00
|
|
|
|
|
|
|
if (autoplay) {
|
|
|
|
// start playing new song
|
|
|
|
player.play();
|
|
|
|
}
|
2016-11-28 16:11:04 -05:00
|
|
|
}
|