@@ -10,6 +10,7 @@ var File = require('../File');
1010var FileTypesManager = require ( '../FileTypesManager' ) ;
1111var GetFastValue = require ( '../../utils/object/GetFastValue' ) ;
1212var HTML5AudioFile = require ( './HTML5AudioFile' ) ;
13+ var IsPlainObject = require ( '../../utils/object/IsPlainObject' ) ;
1314
1415/**
1516 * @classdesc
@@ -33,25 +34,27 @@ var AudioFile = new Class({
3334
3435 initialize :
3536
36- function AudioFile ( loader , key , url , xhrSettings , audioContext )
37+ // URL is an object created by AudioFile.findAudioURL
38+ function AudioFile ( loader , key , urlConfig , xhrSettings , audioContext )
3739 {
38- /**
39- * [description]
40- *
41- * @name Phaser.Loader.FileTypes.AudioFile#context
42- * @type { AudioContext }
43- * @since 3.0.0
44- */
45- this . context = audioContext ;
40+ if ( IsPlainObject ( key ) )
41+ {
42+ var config = key ;
43+
44+ key = GetFastValue ( config , 'key' ) ;
45+ xhrSettings = GetFastValue ( config , 'xhrSettings' ) ;
46+ audioContext = GetFastValue ( config , 'context' , audioContext ) ;
47+ }
4648
4749 var fileConfig = {
4850 type : 'audio' ,
4951 cache : loader . cacheManager . audio ,
50- extension : GetFastValue ( url , ' type' , '' ) ,
52+ extension : urlConfig . type ,
5153 responseType : 'arraybuffer' ,
5254 key : key ,
53- url : GetFastValue ( url , 'uri' , url ) ,
54- xhrSettings : xhrSettings
55+ url : urlConfig . url ,
56+ xhrSettings : xhrSettings ,
57+ config : { context : audioContext }
5558 } ;
5659
5760 File . call ( this , loader , fileConfig ) ;
@@ -62,70 +65,99 @@ var AudioFile = new Class({
6265 *
6366 * @method Phaser.Loader.FileTypes.AudioFile#onProcess
6467 * @since 3.0.0
65- *
66- * @param {FileProcessCallback } callback - [description]
6768 */
68- onProcess : function ( callback )
69+ onProcess : function ( )
6970 {
7071 this . state = CONST . FILE_PROCESSING ;
7172
7273 var _this = this ;
7374
7475 // interesting read https://github.com/WebAudio/web-audio-api/issues/1305
75- this . context . decodeAudioData ( this . xhrLoader . response ,
76+ this . config . context . decodeAudioData ( this . xhrLoader . response ,
7677 function ( audioBuffer )
7778 {
7879 _this . data = audioBuffer ;
7980
80- _this . onComplete ( ) ;
81-
82- callback ( _this ) ;
81+ _this . onProcessComplete ( ) ;
8382 } ,
8483 function ( e )
8584 {
8685 // eslint-disable-next-line no-console
87- console . error ( 'Error with decoding audio data for \'' + this . key + '\': ' , e . message ) ;
86+ console . error ( 'Error decoding audio: ' + this . key + ' - ' , e . message ) ;
8887
89- _this . state = CONST . FILE_ERRORED ;
90-
91- callback ( _this ) ;
88+ _this . onProcessError ( ) ;
9289 }
9390 ) ;
9491
95- this . context = null ;
92+ this . config . context = null ;
9693 }
9794
9895} ) ;
9996
100- AudioFile . create = function ( loader , key , urls , config , xhrSettings )
97+ function createAudio ( loader , key , urls , config , xhrSettings )
10198{
10299 var game = loader . systems . game ;
103100 var audioConfig = game . config . audio ;
104101 var deviceAudio = game . device . audio ;
105102
106- if ( ( audioConfig && audioConfig . noAudio ) || ( ! deviceAudio . webAudio && ! deviceAudio . audioData ) )
103+ // url may be inside key, which may be an object
104+ if ( IsPlainObject ( key ) )
107105 {
108- // console.info('Skipping loading audio \'' + key + '\' since sounds are disabled.' );
109- return null ;
106+ urls = GetFastValue ( key , 'url' , [ ] ) ;
107+ config = GetFastValue ( key , 'config' , { } ) ;
110108 }
111109
112- var url = AudioFile . findAudioURL ( game , urls ) ;
110+ var urlConfig = findAudioURL ( game , urls ) ;
113111
114- if ( ! url )
112+ if ( ! urlConfig )
115113 {
116- // console.warn('No supported url provided for audio \'' + key + '\'!');
117114 return null ;
118115 }
119116
117+ // https://developers.google.com/web/updates/2012/02/HTML5-audio-and-the-Web-Audio-API-are-BFFs
118+ var stream = GetFastValue ( config , 'stream' , false ) ;
119+
120120 if ( deviceAudio . webAudio && ! ( audioConfig && audioConfig . disableWebAudio ) )
121121 {
122- return new AudioFile ( loader , key , url , xhrSettings , game . sound . context ) ;
122+ return new AudioFile ( loader , key , urlConfig , xhrSettings , game . sound . context ) ;
123123 }
124124 else
125125 {
126- return new HTML5AudioFile ( loader , key , url , config ) ;
126+ return new HTML5AudioFile ( loader , key , urlConfig , config ) ;
127+ }
128+ }
129+
130+ function findAudioURL ( game , urls )
131+ {
132+ if ( ! Array . isArray ( urls ) )
133+ {
134+ urls = [ urls ] ;
135+ }
136+
137+ for ( var i = 0 ; i < urls . length ; i ++ )
138+ {
139+ var url = GetFastValue ( urls [ i ] , 'url' , urls [ i ] ) ;
140+
141+ if ( url . indexOf ( 'blob:' ) === 0 || url . indexOf ( 'data:' ) === 0 )
142+ {
143+ return url ;
144+ }
145+
146+ var audioType = url . match ( / \. ( [ a - z A - Z 0 - 9 ] + ) ( $ | \? ) / ) ;
147+
148+ audioType = GetFastValue ( urls [ i ] , 'type' , ( audioType ) ? audioType [ 1 ] : '' ) . toLowerCase ( ) ;
149+
150+ if ( game . device . audio [ audioType ] )
151+ {
152+ return {
153+ url : url ,
154+ type : audioType
155+ } ;
156+ }
127157 }
128- } ;
158+
159+ return null ;
160+ }
129161
130162/**
131163 * Adds an Audio file to the current load queue.
@@ -147,90 +179,40 @@ AudioFile.create = function (loader, key, urls, config, xhrSettings)
147179 */
148180FileTypesManager . register ( 'audio' , function ( key , urls , config , xhrSettings )
149181{
150- var audioFile = AudioFile . create ( this , key , urls , config , xhrSettings ) ;
151-
152- if ( audioFile )
153- {
154- this . addFile ( audioFile ) ;
155- }
156-
157- return this ;
158- } ) ;
182+ var game = this . systems . game ;
183+ var audioConfig = game . config . audio ;
184+ var deviceAudio = game . device . audio ;
159185
160- // this.load.audio('sound', 'assets/audio/booom.ogg', config, xhrSettings);
161- //
162- // this.load.audio('sound',
163- // [
164- // 'assets/audio/booom.ogg',
165- // 'assets/audio/booom.m4a',
166- // 'assets/audio/booom.mp3'
167- // ],
168- // config, xhrSettings);
169- //
170- // this.load.audio('sound',
171- // {
172- // uri: 'assets/audio/boooooom',
173- // type: 'ogg'
174- // },
175- // config, xhrSettings);
176- //
177- // this.load.audio('sound',
178- // [
179- // {
180- // uri: 'assets/audio/booooooo',
181- // type: 'ogg'
182- // },
183- // {
184- // uri: 'assets/audio/boooooom',
185- // type: 'mp3'
186- // }
187- // ],
188- // config, xhrSettings);
189- //
190- // this.load.audio('sound',
191- // [
192- // {
193- // uri: 'assets/audio/booooooo',
194- // type: 'ogg'
195- // },
196- // 'assets/audio/booom.m4a',
197- // {
198- // uri: 'assets/audio/boooooom',
199- // type: 'mp3'
200- // }
201- // ],
202- // config, xhrSettings);
203-
204- AudioFile . findAudioURL = function ( game , urls )
205- {
206- if ( urls . constructor !== Array )
186+ if ( ( audioConfig && audioConfig . noAudio ) || ( ! deviceAudio . webAudio && ! deviceAudio . audioData ) )
207187 {
208- urls = [ urls ] ;
188+ // Sounds are disabled, so skip loading audio
189+ return this ;
209190 }
210191
211- for ( var i = 0 ; i < urls . length ; i ++ )
192+ if ( Array . isArray ( key ) )
212193 {
213- var url = GetFastValue ( urls [ i ] , 'uri' , urls [ i ] ) ;
214-
215- if ( url . indexOf ( 'blob:' ) === 0 || url . indexOf ( 'data:' ) === 0 )
194+ for ( var i = 0 ; i < key . length ; i ++ )
216195 {
217- return url ;
218- }
219-
220- var audioType = url . match ( / \. ( [ a - z A - Z 0 - 9 ] + ) ( $ | \? ) / ) ;
196+ // If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
197+ var audioFile = createAudio ( this , key [ i ] ) ;
221198
222- audioType = GetFastValue ( urls [ i ] , 'type' , ( audioType ) ? audioType [ 1 ] : '' ) . toLowerCase ( ) ;
199+ if ( audioFile )
200+ {
201+ this . addFile ( audioFile ) ;
202+ }
203+ }
204+ }
205+ else
206+ {
207+ var audioFile = createAudio ( this , key , urls , config , xhrSettings ) ;
223208
224- if ( game . device . audio [ audioType ] )
209+ if ( audioFile )
225210 {
226- return {
227- uri : url ,
228- type : audioType
229- } ;
211+ this . addFile ( audioFile ) ;
230212 }
231213 }
232214
233- return null ;
234- } ;
215+ return this ;
216+ } ) ;
235217
236218module . exports = AudioFile ;
0 commit comments