website/public/js/music.js

132 lines
3.4 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-29 00:04:40 -05:00
// set the player's volume to 0.5 by default
player.volume = 0.5;
// on music track click
2016-11-29 00:02:29 -05:00
$('.now-playing-list').on('click', '.music-track', function() {
// change selected track
changeSelectedTrack(this);
2016-11-28 16:11:04 -05:00
});
// on album art click
2016-11-29 00:01:42 -05:00
$('.music-album').on('click', '.thumbnail', function() {
// change selected album
changeSelectedAlbum(this);
});
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');
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
$('#album-title').text(json.title);
// change album description
$('#album-description').text(json.description);
});
}
function getAlbumSongs(albumElem) {
2016-11-28 23:28:04 -05:00
albumElem = $(albumElem).parent();
$.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');
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);
if (i === 0) {
2016-11-28 23:47:11 -05:00
songElem.addClass('selected');
}
2016-11-28 23:47:11 -05:00
songElem.html('<span>' + song.track_order + '. ' + song.title + '</span>');
2016-11-28 23:47:11 -05:00
playlist.append(songElem);
}
2016-11-28 23:47:11 -05:00
2016-11-28 23:51:33 -05:00
changeAudioSources(playlist.find('.selected'), false);
});
}
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 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() {
// 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 23:51:33 -05:00
if (autoplay) {
// start playing new song
player.play();
}
2016-11-28 16:11:04 -05:00
}