Created
January 14, 2013 22:31
-
-
Save raphaelbastide/4534144 to your computer and use it in GitHub Desktop.
<audio> + random + autoplay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<audio> + random + autoplay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body{ | |
text-align:center; | |
padding:20px 0 0 0 ; | |
font-family:arial; | |
letter-spacing:1px; | |
height:10px; | |
} | |
i{ | |
font-style:normal; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button id="play" class="played">❙❙</button> | |
<audio id="audio" loop="loop" data-duration="29"> | |
<source src="http://upload.wikimedia.org/wikipedia/commons/a/a9/Tromboon-sample.ogg" type="audio/ogg"> | |
<source src="http://www.sounddogs.com/sound-effects/25/mp3/382997_SOUNDDOGS__do.mp3" type="audio/mp3"> | |
</audio> | |
<p class="ran">A commenc? ? <i></i> sec.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Random playback | |
// You have to manually fill the data-duration attribute in HTML audio tag :( | |
// No chrome support yet | |
var audio = $('#audio'), | |
totalTime = audio.attr('data-duration'), | |
startTime = Math.floor(Math.random() * totalTime); | |
console.log(startTime); | |
audio[0].currentTime = startTime; // No Chrome support at this point | |
$(".ran i").prepend(startTime); | |
// Autoplay | |
audio.trigger("play"); | |
// Toggle play / pause on click | |
$('#play').click(function(){ | |
var $this = $(this); | |
if ($this.hasClass('paused')){ | |
$this.removeClass('paused').addClass('played').html('❙❙'); | |
audio.trigger("play"); | |
}else if($this.hasClass('played')){ | |
$this.removeClass('played').addClass('paused').html('▶'); | |
audio.trigger("pause"); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment