1
0
Fork 0
oldhaven/public/js/beathaven/player.js

199 lines
6.3 KiB
JavaScript
Raw Normal View History

2011-06-14 16:05:12 +00:00
var Player = {
bar_width: 330,
2011-06-23 22:49:18 +00:00
jp: null,
scrobbled: false,
2011-06-14 16:05:12 +00:00
2011-06-23 22:49:18 +00:00
initJplayer: function() {
this.jp = $("#jplayer");
this.jp.jPlayer({
swfPath: "/js",
supplied: "mp3",
cssSelectorAncestor: "",
cssSelector: {
play: "#player .play",
pause: "#player .pause",
stop: "",
videoPlay: "",
seekBar: "",
playBar: "",
mute: "",
unmute: "",
volumeBar: "",
volumeBarValue: "",
currentTime: "",
duration: ""
}
});
this.jp.bind($.jPlayer.event.timeupdate, function(e){
data = e.jPlayer.status;
if (!Player.scrobbled && data.currentPercentAbsolute > 50) {
var $obj = $('.playlist-tracks li.now');
Player.scrobble($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
2011-06-23 22:49:18 +00:00
Player.scrobbled = true;
}
$('#player .progress .loaded').width(data.seekPercent * Player.bar_width / 100);
$('#player .progress .played').width(data.currentPercentAbsolute * Player.bar_width / 100);
});
this.jp.bind($.jPlayer.event.ended, function(e){
Player.setTrack(Player.nextTrack());
});
2011-06-14 16:05:12 +00:00
},
addTrack: function(artist, album, track, length, autoplay) {
if (typeof autoplay === 'undefined') {
autoplay = false;
}
var initial_count = $('.playlist-tracks li').length;
2011-06-14 16:05:12 +00:00
$('.playlist-tracks').append('\
<li id="i'+ Math.round(Math.random() * 999999) +'" data-artist="'+ artist +'" data-album="'+ album +'" data-track="'+ track +'" data-length="'+ length +'">\
2011-06-14 16:05:12 +00:00
<div class="item">\
2011-06-16 03:27:07 +00:00
<div class="fade"></div>\
2011-06-14 16:05:12 +00:00
<span class="title">'+ artist +' &mdash; '+ track +'</span>\
<span class="duration">'+ length +'</span>\
<div class="remove">remove</div>\
2011-06-14 16:05:12 +00:00
</div>\
</li>\
');
2011-06-15 01:42:50 +00:00
$('#playlist').html($('.playlist-tracks')).scrollbar();
$('.playlist-tracks').sortable();
if (autoplay) {
Player.setTrack($('.playlist-tracks li').last().attr('id').split('i')[1]);
} else if (initial_count === 0 && !Player.hasTrack()) {
Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]);
}
2011-06-14 16:05:12 +00:00
},
2011-06-22 04:46:10 +00:00
setTrack: function(id) {
var $obj = $('#i'+ id);
var query = $obj.attr('data-artist') +'&mdash;'+ $obj.attr('data-track');
2011-06-14 16:05:12 +00:00
$('#player .now-playing').html(query);
$('.playlist-tracks li').removeClass('now');
$obj.addClass('now');
$('#player .loaded, #player .played').width(0);
2011-06-14 16:05:12 +00:00
loadTracksData($obj.attr('data-artist'), $obj.attr('data-track'), $obj.attr('data-length'), Player.playSource);
Player.updateNowListening($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
2011-06-14 16:05:12 +00:00
},
hasTrack: function() {
if ($('#jplayer audio').length > 0) {
return typeof $('#jplayer audio').attr('src') !== 'undefined';
} else if ($('#jplayer audio').length > 0) {
$('#jplayer').jPlayer('play');
return true;
}
return false;
},
2011-06-14 16:05:12 +00:00
playSource: function(url) {
2011-06-23 22:49:18 +00:00
$('#jplayer').jPlayer('setMedia', {mp3: url}).jPlayer('play');
Player.scrobbled = false;
2011-06-14 16:05:12 +00:00
},
2011-06-14 22:40:01 +00:00
nextTrack: function() {
if (true) { // Shuffle off
if ($('.playlist-tracks .now').next().length == 0 && true) { // Last track in the playlist and repeat is on
return $('.playlist-tracks li').first().attr('id').split('i')[1];
} else {
return $('.playlist-tracks .now').next().attr('id').split('i')[1];
}
}
},
prevTrack: function() {
if (true) { // Shuffle off
if ($('.playlist-tracks .now').prev().length == 0 && true) { // First track in the playlist
return $('.playlist-tracks li').last().attr('id').split('i')[1];
} else {
return $('.playlist-tracks .now').prev().attr('id').split('i')[1];
}
}
},
updateNowListening: function(artist, album, track) {
Session.query('/lastfm/listening?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track})
2011-06-22 23:44:27 +00:00
},
scrobble: function(artist, album, track) {
Session.query('/lastfm/scrobble?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track})
2011-06-14 16:05:12 +00:00
}
}
2011-06-23 22:49:18 +00:00
$(function(){
Player.initJplayer();
});
2011-06-14 22:40:01 +00:00
$('#player .controls .prev').live('click', function(){
Player.setTrack(Player.prevTrack());
});
$('#player .controls .next').live('click', function(){
Player.setTrack(Player.nextTrack());
});
2011-06-14 16:05:12 +00:00
$('.add-album').live('click', function() {
var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
$(this).parent().parent().parent().find('.tracklist li').each(function(i, item){
2011-06-14 16:05:12 +00:00
var track_name = $(item).find('.trackname').html();
var length = $(item).find('.length').html();
Player.addTrack(artist, album, track_name, length);
2011-06-14 16:05:12 +00:00
});
})
$('.add-track').live('click', function(){
var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
var track_name = $(this).parent().find('.trackname').html();
var length = $(this).parent().find('.length').html();
Player.addTrack(artist, album, track_name, length);
});
$('#player .play').live('click', function(){
if ($('.playlist-tracks li').length > 0 && !Player.hasTrack()) {
Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]);
}
});
$('.tracklist li').live('mouseover mouseout', function(e){
if (e.type == 'mouseover') {
$(this).find('.add-track').show();
} else {
$(this).find('.add-track').hide();
}
}).live('dblclick', function(e){
var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
var track_name = $(this).find('.trackname').html();
var length = $(this).find('.length').html();
Player.addTrack(artist, album, track_name, length, true);
});
2011-06-14 16:05:12 +00:00
$('.playlist-tracks li .fade, .playlist-tracks li .duration, .playlist-tracks li .remove').live('mouseover mouseout', function(e){
if (e.type == 'mouseover') {
$(this).parent().find('.duration').hide();
$(this).parent().find('.remove').show();
} else {
$(this).parent().find('.remove').hide();
$(this).parent().find('.duration').show();
}
});
$('.playlist-tracks li .remove').live('click', function(){
var $li = $(this).parent().parent();
if ($li.hasClass('now')) {
Player.reset();
}
$li.remove();
});
2011-06-14 16:05:12 +00:00
$('.playlist-tracks li').live('dblclick', function(){
Player.setTrack($(this).attr('id').split('i')[1]);
});
$('#player .progress').live('click', function(e){
2011-06-23 22:49:18 +00:00
$('#jplayer').jPlayer('playHead', Math.round((e.offsetX / Player.bar_width) * 100));
});