|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Audio Format Experiments</title> |
| 6 | + </head> |
| 7 | + <body> |
| 8 | + <h1>Audio Format Experiments</h1> |
| 9 | + <p>I wanted to see how reliably we can 'loop' audio files. Turns out, it's pretty poor.</p> |
| 10 | + <p>I'm trying to find a better way to work with <audio> that works across multiple browsers to improve my <a href="/labs/html5drums/">HTML5 drum kit</a> toy.</p> |
| 11 | + <p>If you play with this, would you <a href="http://brianarn.wufoo.com/forms/audio-format-survey/">fill out a short survey</a>?</p> |
| 12 | + <ul id="elements"> |
| 13 | + <li><audio id="cowbell_wav" src="cowbell.wav" autobuffer></audio></li> |
| 14 | + <li><audio id="cowbell_mp3" src="cowbell.mp3" autobuffer></audio></li> |
| 15 | + <li><audio id="cowbell_ogg" src="cowbell.ogg" autobuffer></audio></li> |
| 16 | + <li><audio id="cowbell_aif" src="cowbell.aif" autobuffer></audio></li> |
| 17 | + </ul> |
| 18 | + <p><insert requisite "you should <a href="http://twitter.com/brianarn">follow me on Twitter</a>" link here></p> |
| 19 | + <script> |
| 20 | + (function () { |
| 21 | + var sounds, interval, i, l; |
| 22 | + |
| 23 | + // Set up |
| 24 | + interval = 500; |
| 25 | + sounds = document.getElementsByTagName('audio'); |
| 26 | + |
| 27 | + // Build up |
| 28 | + for (i = 0, l = sounds.length; i < l; i++) (function (i) { |
| 29 | + var el, fmt, label, play, rew, cycle, cycling; |
| 30 | + |
| 31 | + // Just because it's easier to work with this way |
| 32 | + el = sounds[i]; |
| 33 | + |
| 34 | + // Get/make some stuff |
| 35 | + fmt = el.id.split('_')[1]; |
| 36 | + label = document.createElement('label'); |
| 37 | + label.innerHTML = fmt; |
| 38 | + play = document.createElement('button'); |
| 39 | + play.innerHTML = 'Play'; |
| 40 | + rew = document.createElement('button'); |
| 41 | + rew.innerHTML = 'Rewind'; |
| 42 | + cycle = document.createElement('button'); |
| 43 | + cycle.innerHTML = 'Loop @ '+interval+'ms'; |
| 44 | + |
| 45 | + // Handlerrrrrrrrs |
| 46 | + play.addEventListener('click', function (e) { |
| 47 | + if (el.paused) { |
| 48 | + play.innerHTML = 'Pause'; |
| 49 | + el.play(); |
| 50 | + } else { |
| 51 | + play.innerHTML = 'Play'; |
| 52 | + el.pause(); |
| 53 | + } |
| 54 | + }, false); |
| 55 | + rew.addEventListener('click', function (e) { |
| 56 | + if (!el.paused) { |
| 57 | + play.innerHTML = 'Play'; |
| 58 | + el.pause(); |
| 59 | + } |
| 60 | + el.currentTime = 0; |
| 61 | + }, false); |
| 62 | + cycle.addEventListener('click', function (e) { |
| 63 | + if (cycle.innerHTML === 'Stop') { |
| 64 | + clearInterval(cycling); |
| 65 | + cycle.innerHTML = 'Loop @ '+interval+'ms'; |
| 66 | + } else { |
| 67 | + cycle.innerHTML = 'Stop'; |
| 68 | + if (!el.paused) { |
| 69 | + play.innerHTML = 'Play'; |
| 70 | + el.pause(); |
| 71 | + } |
| 72 | + cycling = setInterval(function () { |
| 73 | + el.pause(); |
| 74 | + el.currentTime = 0; |
| 75 | + el.play(); |
| 76 | + }, interval); |
| 77 | + } |
| 78 | + }, false); |
| 79 | + el.addEventListener('ended', function (e) { |
| 80 | + // Rewind it |
| 81 | + el.pause(); |
| 82 | + el.currentTime = 0; |
| 83 | + play.innerHTML = 'Play'; |
| 84 | + }, false); |
| 85 | + |
| 86 | + // Put the stuff into the li |
| 87 | + el.parentNode.appendChild(label); |
| 88 | + el.parentNode.appendChild(play); |
| 89 | + el.parentNode.appendChild(rew); |
| 90 | + el.parentNode.appendChild(cycle); |
| 91 | + })(i); |
| 92 | + })(); |
| 93 | + </script> |
| 94 | + </body> |
| 95 | +</html> |
0 commit comments