]> id.pley.net Git - sound.git/commitdiff
Support Audio.preload.
authorJer Noble <jer.noble@apple.com>
Thu, 27 Mar 2014 18:39:34 +0000 (11:39 -0700)
committerJer Noble <jer.noble@apple.com>
Thu, 27 Mar 2014 18:39:34 +0000 (11:39 -0700)
sound.html
sound.js

index 6f0a1f6fe19d7dfb29582244fe4ea61bd7c89c9f..d050524eac5938ee36deb251ae3fd64139972bd7 100644 (file)
@@ -5,7 +5,7 @@
        <script src="sound.js"></script>
        <script>
 
        <script src="sound.js"></script>
        <script>
 
-       var sound;
+       var audio;
 
        function log(text) {
                var log = document.getElementById('log');
 
        function log(text) {
                var log = document.getElementById('log');
        }
 
        function onload() {
        }
 
        function onload() {
-               sound = new Sound();
-               sound.src = 'Coin.wav';
-               sound.autoplay = true;
-               sound.addEventListener('ended', eventLogger);
-               sound.addEventListener('play', eventLogger);
-               sound.addEventListener('pause', eventLogger);
-               sound.addEventListener('playing', eventLogger);
-               sound.addEventListener('timeupdate', eventLogger);
-               sound.addEventListener('waiting', eventLogger);
-               sound.addEventListener('volumechange', eventLogger);
-               sound.addEventListener('emptied', eventLogger);
-               sound.addEventListener('loadstart', eventLogger);
-               sound.addEventListener('progress', eventLogger);
+               audio = new Audio();
+               audio.autoplay = false;
+               audio.src = 'Coin.wav';
+               audio.preload = 'auto';
+               audio.addEventListener('ended', eventLogger);
+               audio.addEventListener('play', eventLogger);
+               audio.addEventListener('pause', eventLogger);
+               audio.addEventListener('playing', eventLogger);
+               audio.addEventListener('timeupdate', eventLogger);
+               audio.addEventListener('waiting', eventLogger);
+               audio.addEventListener('volumechange', eventLogger);
+               audio.addEventListener('emptied', eventLogger);
+               audio.addEventListener('loadstart', eventLogger);
+               audio.addEventListener('progress', eventLogger);
        }
 
        </script>
        }
 
        </script>
 <body onload="onload()">
 
        <div>
 <body onload="onload()">
 
        <div>
-               <button onclick="sound.play()">play</button>
-               <button onclick="sound.pause()">pause</button>
-               <button onclick="sound.muted = !sound.muted">mute</button>
-               <input type="range" min="0" max="1" step="0.01" onchange="sound.volume = event.target.value" />
-               <button onclick="sound.loop = !sound.loop">loop</button>
+               <button onclick="audio.play()">play</button>
+               <button onclick="audio.pause()">pause</button>
+               <button onclick="audio.muted = !audio.muted">mute</button>
+               <input type="range" min="0" max="1" step="0.01" onchange="audio.volume = event.target.value" />
+               <button onclick="audio.loop = !audio.loop">loop</button>
        </div>
 
 </body>
        </div>
 
 </body>
index c4f9c0eb27329c84bbfa0564a678ea1e13f8945f..df5202e61c8911f84561b79acfe39149bc63b58e 100644 (file)
--- a/sound.js
+++ b/sound.js
@@ -1,11 +1,12 @@
-function Sound() {
+function Sound(src) {
 
 
-       if (Sound.audioContext === undefined)
-               Sound.audioContext = new webkitAudioContext();
+       if (Sound.audioContext === undefined) {
+               var AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
+               Sound.audioContext = new AudioContext();
+       }
 
 
-       this._src = null;
        this._networkState = this.NETWORK.EMPTY;
        this._networkState = this.NETWORK.EMPTY;
-       this._preload = true;
+       this._preload = this.PRELOAD.AUTO;
        this._buffered = {};
        this._readyState = this.READY.NOTHING;
        this._seeking = false;
        this._buffered = {};
        this._readyState = this.READY.NOTHING;
        this._seeking = false;
@@ -15,7 +16,7 @@ function Sound() {
        this._played = {};
        this._seekable = {};
        this._ended = false;
        this._played = {};
        this._seekable = {};
        this._ended = false;
-       this._autoplay = false;
+       this._autoplay = true;
        this._loop = false;
        this._volume = 1;
        this._muted = false;
        this._loop = false;
        this._volume = 1;
        this._muted = false;
@@ -26,10 +27,12 @@ function Sound() {
        this.gainNode = null;
 
        this.ajax = null;
        this.gainNode = null;
 
        this.ajax = null;
-       this.eventListeners = { }; 
+       this.eventListeners = { };
        this.shouldBePlaying = 0;
        this.startTime = 0;
        this.nextStartTime = 0;
        this.shouldBePlaying = 0;
        this.startTime = 0;
        this.nextStartTime = 0;
+
+       this.setSrc(src);
 }
 
 Sound.prototype = {
 }
 
 Sound.prototype = {
@@ -57,6 +60,12 @@ Sound.prototype = {
                ENOUGH_DATA: 4,
        },
 
                ENOUGH_DATA: 4,
        },
 
+       PRELOAD: {
+               NONE: 0,
+               METADATA: 1,
+               AUTO: 2,
+       },
+
        load: function() {
                if (this.ajax)
                        this.ajax.abort();
        load: function() {
                if (this.ajax)
                        this.ajax.abort();
@@ -74,7 +83,7 @@ Sound.prototype = {
 
                this.setPlaybackRate(this.defaultPlaybackRate);
                this._error = null;
 
                this.setPlaybackRate(this.defaultPlaybackRate);
                this._error = null;
-               this._autoplay = true;
+               this.shouldBePlaying = this._autoplay;
                this.stopInternal();
 
                if (!this._src) {
                this.stopInternal();
 
                if (!this._src) {
@@ -243,7 +252,7 @@ Sound.prototype = {
 
        setSrc: function(src) {
                this._src = src;
 
        setSrc: function(src) {
                this._src = src;
-               if (this._autoplay && this._src != null)
+               if (this._autoplay || this._preload != this.PRELOAD.NONE)
                        this.load();
        },
 
                        this.load();
        },
 
@@ -264,21 +273,30 @@ Sound.prototype = {
        },
 
        getPreload: function() {
        },
 
        getPreload: function() {
-               if (!this._preload)
-                       return 'none';
-               return 'auto';
+               switch (this._preload) {
+                       case this.PRELOAD.NONE: return 'none';
+                       case this.PRELOAD.METADATA: return 'metadata';
+                       case this.PRELOAD.AUTO: return 'auto';
+                       default: return '';
+               }
        },
 
        setPreload: function(preload) {
                switch (preload) {
        },
 
        setPreload: function(preload) {
                switch (preload) {
-               case 'none':
-                       this._preload = false;
-                       break;
-               default:
-                       this._preload = true;
-                       if (!this.buffer)
-                               load();
-                       break;
+                       default:
+                       case 'none':
+                               this._preload = this.PRELOAD.NONE;
+                               break;
+                       case 'metadata':
+                               this._preload = this.PRELOAD.METADATA;
+                               if (this._networkState === this.NETWORK.EMPTY)
+                                       this.load();
+                               break;
+                       case 'auto':
+                               this._preload = this.PRELOAD.auto;
+                               if (this._networkState === this.NETWORK.EMPTY)
+                                       this.load();
+                               break;
                }
        },
 
                }
        },
 
@@ -288,7 +306,7 @@ Sound.prototype = {
                return this.nextStartTime + Sound.audioContext.currentTIme - this.startTime;
        },
 
                return this.nextStartTime + Sound.audioContext.currentTIme - this.startTime;
        },
 
-       setCurrentTime: function(time) { 
+       setCurrentTime: function(time) {
                this.nextStartTime = time;
                this.dispatchEventAsync(new CustomEvent('timeupdate'));
                if (!this.node)
                this.nextStartTime = time;
                this.dispatchEventAsync(new CustomEvent('timeupdate'));
                if (!this.node)
@@ -327,7 +345,7 @@ Sound.prototype = {
        },
 
        setVolume: function(volume) {
        },
 
        setVolume: function(volume) {
-               if (this._volume == volume)
+               if (this._volume === volume)
                        return;
 
                this._volume = volume;
                        return;
 
                this._volume = volume;
@@ -342,7 +360,7 @@ Sound.prototype = {
        },
 
        setMuted: function(muted) {
        },
 
        setMuted: function(muted) {
-               if (this._muted == muted)
+               if (this._muted === muted)
                        return;
 
                this._muted = muted;
                        return;
 
                this._muted = muted;
@@ -357,11 +375,11 @@ Sound.prototype = {
        },
 
        setAutoplay: function(autoplay) {
        },
 
        setAutoplay: function(autoplay) {
-               if (this._autoplay == autoplay)
+               if (this._autoplay === autoplay)
                        return;
 
                this._autoplay = autoplay;
                        return;
 
                this._autoplay = autoplay;
-               if (this._autoplay && this._src != null)
+               if (this._autoplay && this._networkState === this.NETWORK.EMPTY)
                        this.load();
        },
 
                        this.load();
        },
 
@@ -469,8 +487,17 @@ Object.defineProperty(Sound.prototype, 'defaultMuted', {
        set: Sound.prototype.setDefaultMuted,
 });
 
        set: Sound.prototype.setDefaultMuted,
 });
 
+Object.defineProperty(Sound.prototype, 'preload', {
+       get: Sound.prototype.getPreload,
+       set: Sound.prototype.setPreload,
+});
+
 document.createElement = function(elementName) {
        if (elementName === "Audio" || elementName === "audio")
                return new Sound();
        return Document.prototype.createElement.call(this, elementName);
 document.createElement = function(elementName) {
        if (elementName === "Audio" || elementName === "audio")
                return new Sound();
        return Document.prototype.createElement.call(this, elementName);
+};
+
+window.Audio = function(src) {
+       return new Sound(src);
 };
\ No newline at end of file
 };
\ No newline at end of file