Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //THIS GOES IN YOUR JAVASCRIPT FILE.
- //Create the audiosprite.
- var audioSprite = document.getElementById('audio');
- // the locations of each sound effect, in seconds.
- var spriteData = {
- silence: {
- start: 0,
- length: 1.9
- },
- BookSlide: {
- start: 2,
- length: .9
- },
- BookSlam: {
- start: 3,
- length: .9
- },
- BookOnDesk: {
- start: 4,
- length: .9
- }
- PagesTurned: {
- start: 5,
- length: .9
- }
- ButtonSound: {
- start: 6,
- length: .9
- }
- };
- // current sprite being played
- var currentSprite = {};
- // time updater to ensure that the audio is stopped when a sprite is complete
- var onTimeUpdate = function() {
- if (this.currentTime >= currentSprite.start + currentSprite.length) {
- this.pause();
- }
- };
- //audio listener event.
- audioSprite.addEventListener('timeupdate', onTimeUpdate, false);
- // load the audio.
- var playSprite = function(id) {
- if (spriteData[id] && spriteData[id].length) {
- currentSprite = spriteData[id];
- audioSprite.currentTime = currentSprite.start;
- audioSprite.play();
- }
- //THIS GOES WHEREVER YOU WANT IT TO GO IN YOUR HTML FILE.
- <audio id="audio">
- <source src="(THE NAME AND LOCATION OF YOUR AUDIO SPRITE).m4a" type="audio/mpeg" /> <!--https://soundcloud.com/chris_bob/audio-sprite-example-output is the one used int his file-->
- <source src="http://exampleaudiospritesheet.something.com/booksounds.ogg" type="audio/ogg" />
- </audio>
- <button onclick="playSprite('BookSlide');">Slide the Book Around</button>
- <button onclick="playSprite('BookSlam');">Slam the Book Shut</button>
- <button onclick="playSprite('BookOnDesk');">Slam the Book on the Desk</button>
- <button onclick="playSprite('PagesTurned');">Turn the Pages</button>
- <button onclick="playSprite('ButtonSound');">Play A Button Sound</button>
- <!--www.chrisprunotto.com ; www.chrisprunotto.wordpress.com -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement