var $button = $('.button.play');
var $progress = $('.progress');
var $handle = $('.handle');
var $bar = $('.bar');
var $buffer = $('.buffer');
var $sprite = $('.sprite');
var audio = document.getElementById('audio');
var spriteData = {
meow1: {
start: 0,
length: 1.1
},
meow2: {
start: 1.3,
length: 1.1
},
whine: {
start: 2.7,
length: .8
},
purr: {
start: 5,
length: 5
}
};
var spriteObject = new AudioSprite(audio, spriteData);
spriteObject
.on('meow2:complete', function() {
this.play('purr');
})
.on('purr:complete', function() {
this.play('meow1');
})
.on('meow1:complete', function() {
this.play('whine');
})
.on('whine:complete', function() {
this.play('meow2');
})
;
spriteObject
.on('progress', function(time) {
// round time!
var currentTime = ~~(time * 100) / 100;
var percent = ~~((time / this.el.duration) * 100);
$progress.text(currentTime);
$bar.css('width', percent + '%');
$handle.css('left', percent + '%');
})
.on('play', function() {
$button.text('Pause');
if (this._current) {
$sprite.text('Sprite ID: ' + this._current);
}
})
.on('stop', function() {
$button.text('Play');
})
.on('onload', function(percent) {
$progress.text('Loading: ' + percent + '%');
$buffer.css('width', percent + '%');
})
.on('onloaded', function() {
$progress.text('Loaded: 100%');
})
.on('loadstart', function() {
$progress.text('Initializing');
})
;
$button.on('click', function(e) {
e.preventDefault();
if (audio.ended || audio.paused) {
spriteObject.play();
} else {
spriteObject.stop();
}
});
$button
.on('touchstart', function () {
$(this).addClass('active');
})
.on('touchend touchcancel touchmove', function() {
$(this).removeClass('active');
})
;
<h1>Audio Sprite</h1>
<div class="wrapper">
<a href="#" class="button play">Play</a>
<div class="progressbar">
<div class="bar"></div>
<div class="buffer"></div>
<div class="handle"></div>
</div>
<span class="progress"></span>
<span class="sprite"></span>
<audio id="audio" preload="none">
<source src="http://dl.dropbox.com/u/1538714/article_resources/cat.m4a" type="audio/mpeg" />
<source src="http://dl.dropbox.com/u/1538714/article_resources/cat.ogg" type="audio/ogg" />
</audio>
</div>
* {
padding: 0;
margin: 0;
}
body {
-webkit-tap-highlight-color: rgba(0,0,0,0);
font-family: Arial;
font-size: 16px;
}
h1 {
font-size: 24px;
width: 820px;
margin: 20px auto;
}
h2 {
margin: 10px 0 5px;
}
.wrapper {
margin: 20px auto;
width: 800px;
background: #CCC;
padding: 20px;
}
.wrapper audio {
width: 800px;
}
.button {
width: 50px;
text-align: center;
}
.progress {
display: inline-block;
font-family: monospace;
margin: 0 5px;
min-width: 50px;
}
.sprite {
display: inline-block;
font-family: monospace;
margin: 0 5px;
}
.progressbar {
position: relative;
width: 350px;
height: 20px;
margin: 0 5px;
border: 1px solid #BBB;
display: inline-block;
vertical-align: middle;
}
.progressbar .handle {
position: absolute;
width: 10px;
height: 24px;
top: -2px;
margin-left: -5px;
background: #FFF;
box-shadow: 0 0 5px rgba(0,0,0,0.4);
z-index: 3;
}
.progressbar .buffer {
position: absolute;
left: 0;
width: 0;
height: 20px;
background: #AAA;
z-index: 1;
}
.progressbar .bar {
position: absolute;
left: 0;
width: 0;
height: 20px;
background: #777;
z-index: 2;
}
.button {
display: inline-block;
white-space: nowrap;
background-color: #ccc;
background-image: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#ccc));
background-image: -webkit-linear-gradient(top, #eee, #ccc);
background-image: -moz-linear-gradient(top, #eee, #ccc);
background-image: -ms-linear-gradient(top, #eee, #ccc);
background-image: -o-linear-gradient(top, #eee, #ccc);
background-image: linear-gradient(top, #eee, #ccc);
border: 1px solid #777;
padding: 6px 16px;
margin: 0 5px;
font-family: Arial, Helvetica;
text-decoration: none;
color: #333;
text-shadow: 0 1px 0 rgba(255,255,255,.8);
-moz-border-radius: .2em;
-webkit-border-radius: .2em;
border-radius: .2em;
-moz-box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
-webkit-box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
}
.button:hover {
background-color: #ddd;
background-image: -webkit-gradient(linear, left top, left bottom, from(#fafafa), to(#ddd));
background-image: -webkit-linear-gradient(top, #fafafa, #ddd);
background-image: -moz-linear-gradient(top, #fafafa, #ddd);
background-image: -ms-linear-gradient(top, #fafafa, #ddd);
background-image: -o-linear-gradient(top, #fafafa, #ddd);
background-image: linear-gradient(top, #fafafa, #ddd);
}
.button:active,
.button.active {
-moz-box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
-webkit-box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
position: relative;
top: 1px;
}
.button:focus {
outline: 0;
background: #fafafa;
}
.button:before {
background: #ccc;
background: rgba(0,0,0,.1);
float: left;
width: 1em;
text-align: center;
font-size: 1.5em;
margin: 0 1em 0 -1em;
padding: 0 .2em;
-moz-box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
-webkit-box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
-moz-border-radius: .15em 0 0 .15em;
-webkit-border-radius: .15em 0 0 .15em;
border-radius: .15em 0 0 .15em;
pointer-events: none;
}