Skip to content

Instantly share code, notes, and snippets.

@tomsseisums
Created January 24, 2012 23:00
Show Gist options
  • Save tomsseisums/1673294 to your computer and use it in GitHub Desktop.
Save tomsseisums/1673294 to your computer and use it in GitHub Desktop.
sevensounds player, needs optimization, probably old
$(function(){
var seek = false, volume = false, _playing = false, muted = false, _playlist, _songID = 0, oldposition = 0, largeplaylist;
function getRandomNumber(range){
return Math.floor(Math.random() * range);
}
function getRandomChar(){
var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
return chars.substr( getRandomNumber(62), 1 );
}
function randomID(size){
var str = "";
for(var i = 0; i < size; i++){
str += getRandomChar();
}
return str;
}
$('a.contactme').live('click', function(){
$this = $(this);
$(this).colorbox({
iframe: true,
href: '/contact.php?song=' + $this.next().attr('rel'),
innerWidth: 600,
innerHeight: 440,
title: $this.next().text()
});
return false;
});
/* GALLERY */
$('#content').delegate('.gallery', 'pageload', function(){
var $gallery = $('.gallery');
var $image = $gallery.find('#mainimage');
$gallery.find('.next, .previous').hide();
$gallery.find('a.thumbnail').click(function(){
var img = $gallery.children('img').attr('src');
var largeimage = $(this).children('img').attr('src');
largeimage = largeimage.replace('_thumbs/_', '');
img = largeimage;
$image.children('img').hide().attr('src', img).stop(true, true).fadeIn();
return false;
});
$('#mainimage').bind('click', function(){
// allow to scroll through images while clicking on large image
swapimage(true);
return false;
});
$gallery.find('.next').css('right', '-10px').live({
mouseover : function(){ $(this).stop().animate({ right : '-25px' }, 100); },
mouseout : function(){ $(this).stop().animate({ right : '-10px' }, 100); }
});
$gallery.find('.previous').css('left', '-10px').live({
mouseover : function(){ $(this).stop().animate({ left : '-25px' }, 100); },
mouseout : function(){ $(this).stop().animate({ left : '-10px' }, 100); }
});
$gallery.find('#next-image').click(function(){ swapimage(true); return false; });
$gallery.find('#prev-image').click(function(){ swapimage(false); return false; });
function swapimage(fw){
var imagesrc = $image.children('img').attr('src');
imagesrc = imagesrc.replace('/images/', '/images/_thumbs/_');
var current = $gallery.find('.images img[src="'+ imagesrc +'"]').parent().parent();
if(fw){
var next = current.next();
}else{
var next = current.prev();
}
if(next.length == 0){
if(fw){
next = $gallery.find('.images li').eq(0);
}else{
next = $gallery.find('.images li:last-child');
}
}
var img = next.find('img').attr('src');
img = img.replace('_thumbs/_', '');
$image.children('img').hide().attr('src', img).stop(true, true).fadeIn();
}
});
/* AUDIO PLAYER */
$('#content').delegate('.player', 'pageload', function(){
var templateVars = {}, _playlistID = 0, songCount = 0, sCount = 0, firstSong = 0, songIDs = [];
var player = $(this);
var playlists = [];
var realTemplate = {
backgroundHolder : player.find('.background'),
playlistTitle : player.find('h2'),
elapsedTime : player.find('.scroller .current'),
seekTime : player.find('.scroller .seek'),
songLength : player.find('.scroller .maximum'),
volume : player.find('.volume .volume-value'),
scroller : player.find('.scroller .fill-spoiler .fill'),
volumeSlider : player.find('.volume .fill-spoiler .fill'),
scrollerTip : player.find('.scroller .fill-spoiler .soundtip'),
playButton : player.find('.button.play'),
socialBlock : player.find('.social'),
playlist : player.find('.songlist')
};
templateVars = $.extend(templateVars, realTemplate);
initiatePlayer();
if(player.find('.playlist').length == 1) player.find('.next, .previous').remove(); // there is no such thing as next/previous if only one song is loaded
player.find('.soundtip').hide(); // hide on load
/*templateVars.socialBlock.css('top', '-25px').live({
mouseover : function(){ $(this).stop().animate({ top : '0px' }, 100); },
mouseout : function(){ $(this).stop().animate({ top : '-25px' }, 100); }
});*/
player.find('.next').css('right', '-10px').bind({
mouseover : function(){ $(this).stop().animate({ right : '-25px' }, 100); },
mouseout : function(){ $(this).stop().animate({ right : '-10px' }, 100); },
click : function(){ switchPlaylist(true); return false; }
});
player.find('.previous').css('left', '-10px').bind({
mouseover : function(){ $(this).stop().animate({ left : '-25px' }, 100); },
mouseout : function(){ $(this).stop().animate({ left : '-10px' }, 100); },
click : function(){ switchPlaylist(false); return false; }
});
/*$(this).find('.window').live({
mouseenter : function(){
if(_playing) $(this).children('.play, .scroller, .volume, .social').stop(true, true).fadeIn();
},
mouseleave : function(){
if(_playing) $(this).children('.play, .scroller, .volume, .social').stop(true, true).fadeOut();
}
});*/
player.find('.scroller .fill-spoiler').slider({
start : function(e, ui){ if(_currentSong && _currentSong.playState == 1){ $(this).children('.soundtip').stop(true, true).fadeIn(); seek = true; } },
slide : function(e, ui){
if(_currentSong && _currentSong.playState == 1){
templateVars.scroller.stop(true, true).css('width', ui.value + '%');
templateVars.scrollerTip.css('left', ui.value + '%');
songSeek(ui.value);
}
},
change : function(e, ui){ if(_currentSong && _currentSong.playState == 1){ $(this).children('.soundtip').stop(true, true).fadeOut(); seek = false; } }
}).bind({
mouseover : function(e){
if(_currentSong && _currentSong.playState == 1){
if($(this).children('.soundtip').filter(':hidden') && !seek){
var offset = $(this).offset();
var x = Math.floor(e.pageX - offset.left);
var percentage = Math.floor(x * 100 / $(this).width());
if(percentage > 0 && percentage < 100) $(this).children('.soundtip').css('left', percentage + '%');
$(this).children('.soundtip').stop(true, true).fadeIn();
templateVars.seekTime.html(percentToTime(percentage));
}
}
},
mousemove : function(e){
if(_currentSong && _currentSong.playState == 1){
if($(this).children('.soundtip').filter(':visible') && !seek){
var offset = $(this).offset();
var x = Math.floor(e.pageX - offset.left);
var percentage = Math.floor(x * 100 / $(this).width());
if(percentage > 0 && percentage < 100) $(this).children('.soundtip').css('left', percentage + '%');
if(_currentSong && _currentSong.playState == 1) templateVars.seekTime.html(percentToTime(percentage));
}
}
},
mouseout : function(e){
if(_currentSong && _currentSong.playState == 1){
if($(this).children('.soundtip').filter(':visible') && !seek){
$(this).children('.soundtip').stop(true, true).fadeOut(400, function(){
if(_currentSong && _currentSong.playState == 1) templateVars.seekTime.html('');
});
}
}
}
});
player.find('.volume .control').click(function(){
if(!_currentSong.muted){
$(this).addClass('muted');
$(this).parent().find('.fill-spoiler').stop(true, true).fadeOut();
_currentSong.mute();
}else{
$(this).removeClass('muted');
$(this).parent().find('.fill-spoiler').stop(true, true).fadeIn();
_currentSong.unmute();
}
return false;
});
player.find('.play').click(function(){
if(_currentSong.playState == 1 && !_currentSong.paused){
_currentSong.pause();
}else{
if(_currentSong.playState){
_currentSong.resume();
}else{
_currentSong.play();
}
}
return false;
});
player.find('.volume .fill-spoiler').slider({
orientation : 'vertical',
start : function(e, ui){ $(this).children('.soundtip').stop(true, true).fadeIn(); volume = true; },
slide : function(e, ui){
templateVars.volumeSlider.css('height', ui.value + '%');
templateVars.volume.html(ui.value + '%');
_currentSong.setVolume(ui.value);
volumeIcon(ui.value);
},
change : function(e, ui){ $(this).children('.soundtip').stop(true, true).fadeOut(); volume = false; },
});
function volumeIcon(value){
var icon = $('.volume .control');
if(value == 0){ icon.removeClass('silent medium'); icon.addClass('none'); }
else if(value > 0 && value <= 33){ icon.removeClass('none medium'); icon.addClass('silent'); }
else if(value > 33 && value <= 66){ icon.removeClass('none silent'); icon.addClass('medium'); }
else { icon.removeClass('none silent medium'); }
}
function initiatePlayer(){
soundManager.onready(function(){
var songs = player.find('a.song');
songCount = songs.length;
sCount = player.find('.playlist').eq(_playlistID).children('ul').children().length;
$.each(songs, function(index, value){
var songid = randomID(20);
songIDs.push(songid);
soundManager.createSound({
id: songid,
url: '/uploads/mp3/' + $(this).attr('rel'),
whileloading : function(){ songTimeUpdate(); },
onplay : function() { templateVars.playButton.addClass('paused'); if(currentSong.playState == 1){ $('#pause').trigger('click'); } },
onresume : function() { templateVars.playButton.addClass('paused'); },
onpause : function() { templateVars.playButton.removeClass('paused'); },
whileplaying : function() { songElapsedUpdate(); },
onfinish : function() { songSkip(index); }
});
$(this).click(function(){
if(_currentSong && _currentSong.sID == songid && _currentSong.playState == 1) return false;
templateVars.playlist.find('.active').removeClass('active');
$(this).addClass('active');
if(_currentSong && _currentSong.playState == 1) _currentSong.stop();
_currentSong = soundManager.play(songid);
songTimeUpdate();
return false;
});
});
_currentSong = soundManager.load(songIDs[0]);
templateVars.playlist.find('a.song').eq(0).addClass('active');
});
}
function switchPlaylist(next){
total = player.find('.playlist');
next ? _playlistID++ : _playlistID--;
if(_playlistID == -1) _playlistID = total.last().index();
total.not('.hidden').addClass('hidden');
total.eq(_playlistID).removeClass('hidden');
sCount = total.eq(_playlistID).children('ul').children().length;
songID = parseInt(songCount - sCount);
if(sCount.length == 0 || _playlistID == total.length){
total.eq(0).removeClass('hidden');
_playlistID = 0;
songID = 0;
}
firstSong = songID;
templateVars.playButton.removeClass('paused');
/* MUTE, VOLUME */
if(_currentSong.muted) var nextMute = true;
var preserve_volume = _currentSong.volume;
_currentSong.stop();
songElapsedUpdate(true);
_currentSong = soundManager.load(songIDs[songID]);
player.find('.active').removeClass('active');
player.find('a.song').eq(songID).addClass('active');
if(nextMute) _currentSong.mute();
_currentSong.setVolume(preserve_volume);
}
function songSkip(song){
if(_currentSong.muted) var nextMute = true;
var preserve_volume = _currentSong.volume;
total = player.find('.playlist:not(".hidden")').children('ul').children();
var next = song + 1;
if(next == total.length) next = firstSong;
_currentSong = soundManager.play(songIDs[next]);
player.find('.active').removeClass('active');
player.find('a.song').eq(next).addClass('active');
if(nextMute) _currentSong.mute();
_currentSong.setVolume(preserve_volume);
}
function songTimeUpdate(){
var d = new Date(_currentSong.duration);
seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
templateVars.songLength.html(d.getMinutes() + ':' + seconds);
}
function songSeek(position){
if(_currentSong.playState == 1){
var nP = Math.floor((_currentSong.duration * position) / 100);
_currentSong.setPosition(nP);
}
songElapsedUpdate();
}
function percentToTime(position){
var newTime = Math.floor((_currentSong.duration * position) / 100);
var d = new Date(newTime);
seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
return d.getMinutes() + ':' + seconds + ' &mdash; ';
}
function songElapsedUpdate(reset){
var percentage = (_currentSong.position / _currentSong.duration) * 100;
if(isNaN(percentage)) percentage = 0; // probably song scrolled, reset
if(reset === true) percentage = 0;
templateVars.scroller.css('width', percentage + '%');
if(_currentSong.position >= 0){
time = new Date(_currentSong.position);
seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
templateVars.elapsedTime.html(time.getMinutes() + ':' + seconds);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment