Skip to content

Instantly share code, notes, and snippets.

@alexanderlperez
Created November 1, 2013 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderlperez/7260328 to your computer and use it in GitHub Desktop.
Save alexanderlperez/7260328 to your computer and use it in GitHub Desktop.
Barebones usage of HTML5 audio playback
<style>
.art {
height: 400px;
width: 400px;
background: url('http://i.imgur.com/EQl5vLz.jpg') no-repeat;
}
.button {
position: absolute;
top: 2.5em;
left: 2.5em;
width: 140px;
height: 140px;
background: url('http://i.imgur.com/6C53awK.png') no-repeat;
background-size: contain;
background-position: 0 4px;
z-index: 2;
-webkit-box-shadow: 0px 10px 10px #000;
border-radius: 100%;
}
.button:hover {
background: url('http://i.imgur.com/ISHWYFF.png') no-repeat;
background-size: contain;
}
</style>
<script type="text/javascript">
var toggle = false,
$button = $('.button');
var player = new Audio();
player.src = "http://www.w3schools.com/html/horse.ogg"
/************** button behaviors ****************/
//change from play to stop when clicked, and stop to play when clicked again
//(toggle:true :: playing: true)
$('.button').click(function () {
if (!toggle) {
$(this).css({
'background' : 'url("http://i.imgur.com/UirHLba.png")',
'background-size': 'contain',
'background-position': '0 4px'
});
player.play();
toggle = true;
} else {
$(this).css({
'background' : 'url("http://i.imgur.com/6C53awK.png")',
'background-size': 'contain',
'background-position': '0 4px'
});
toggle = false;
}
});
//only highlight the play button if it's not already playing
//(toggle:true :: playing: true)
$('.button').hover(function () {
if(!toggle){
$(this).css({
'background': "url('http://i.imgur.com/ISHWYFF.png')",
'background-size': 'contain',
'background-position': '0 4px'
});
}
}, function () {
if(!toggle){
$(this).css({
'background': "url('http://i.imgur.com/6C53awK.png')",
'background-size': 'contain',
'background-position': '0 4px'
});
}
});
//button shadow effects
$('html').mouseup(function (){ $('.button').css('-webkit-box-shadow', '0 10px 10px #000') });
$('.button').mousedown(function () { $(this).css('-webkit-box-shadow', '0 0 0'); })
.mouseleave(function () {
$('.button').css({
'-webkit-box-shadow': '0 10px 10px #000',
});
});
</script>
<div class="song-container">
<div class="art"></div>
<div class="button"></div>
<div class="hover-container">
<div class="song-info"></div>
</div>
</div>
@alexanderlperez
Copy link
Author

The actual HTML5 audio functionality is contained in the call to the instantiated Audio object, and the subsequent call to player.play()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment