@vpmedia/phaser 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vpmedia/phaser",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "@vpmedia/phaser is the modern ECMAScript port of the popular Phaser game engine v2.6.2",
5
5
  "author": "Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)",
6
6
  "license": "MIT",
@@ -131,15 +131,13 @@ export default class {
131
131
  this._resolveURL(url, obj);
132
132
  }
133
133
 
134
- addSound(key, url, data, webAudio = true, audioTag = false) {
135
- const decoded = audioTag;
134
+ addSound(key, url, data) {
136
135
  this._cache.sound[key] = {
137
136
  url,
138
137
  data,
139
138
  isDecoding: false,
140
- decoded,
141
- webAudio,
142
- audioTag,
139
+ decoded: false,
140
+ webAudio: true,
143
141
  locked: this.game.sound.touchLocked,
144
142
  };
145
143
  this._resolveURL(url, this._cache.sound[key]);
@@ -33,7 +33,6 @@ export default class {
33
33
  this.safariVersion = 0;
34
34
  this.webApp = false;
35
35
  this.silk = false;
36
- this.audioData = false;
37
36
  this.webAudio = false;
38
37
  this.ogg = false;
39
38
  this.opus = false;
@@ -232,7 +232,6 @@ export function checkVideo(device) {
232
232
  * @param {object} device TBD
233
233
  */
234
234
  export function checkAudio(device) {
235
- device.audioData = !!(window.Audio);
236
235
  device.webAudio = !!(window.AudioContext || window.webkitAudioContext);
237
236
  const audioElement = document.createElement('audio');
238
237
  try {
@@ -627,12 +627,7 @@ export default class {
627
627
  case 'audio':
628
628
  file.url = this.getAudioURL(file.url);
629
629
  if (file.url) {
630
- // WebAudio or Audio Tag?
631
- if (this.game.sound.usingWebAudio) {
632
- this.xhrLoad(file, this.transformUrl(file.url, file), 'arraybuffer', this.fileComplete);
633
- } else if (this.game.sound.usingAudioTag) {
634
- this.loadAudioTag(file);
635
- }
630
+ this.xhrLoad(file, this.transformUrl(file.url, file), 'arraybuffer', this.fileComplete);
636
631
  } else {
637
632
  this.fileError(file, null, 'No supported audio URL specified or device does not have audio playback support');
638
633
  }
@@ -716,35 +711,6 @@ export default class {
716
711
  console.warn('loader.loadVideoTag() is not implemented');
717
712
  }
718
713
 
719
- loadAudioTag(file) {
720
- const scope = this;
721
- if (this.game.sound.touchLocked) {
722
- // If audio is locked we can't do this yet, so need to queue this load request. Bum.
723
- file.data = new Audio();
724
- file.data.name = file.key;
725
- file.data.preload = 'auto';
726
- file.data.src = this.transformUrl(file.url, file);
727
- this.fileComplete(file);
728
- } else {
729
- file.data = new Audio();
730
- file.data.name = file.key;
731
- const playThroughEvent = () => {
732
- file.data.removeEventListener('canplaythrough', playThroughEvent, false);
733
- file.data.onerror = null;
734
- scope.fileComplete(file);
735
- };
736
- file.data.onerror = () => {
737
- file.data.removeEventListener('canplaythrough', playThroughEvent, false);
738
- file.data.onerror = null;
739
- scope.fileError(file);
740
- };
741
- file.data.preload = 'auto';
742
- file.data.src = this.transformUrl(file.url, file);
743
- file.data.addEventListener('canplaythrough', playThroughEvent, false);
744
- file.data.load();
745
- }
746
- }
747
-
748
714
  xhrLoad(file, url, type, onload, onerror) {
749
715
  const xhr = new XMLHttpRequest();
750
716
  xhr.open('GET', url, true);
@@ -904,14 +870,10 @@ export default class {
904
870
  this.cache.addVideo(file.key, file.url, file.data, file.asBlob);
905
871
  break;
906
872
  case 'audio':
907
- if (this.game.sound.usingWebAudio) {
908
- file.data = xhr.response;
909
- this.cache.addSound(file.key, file.url, file.data, true, false);
910
- if (file.autoDecode) {
911
- this.game.sound.decode(file.key);
912
- }
913
- } else {
914
- this.cache.addSound(file.key, file.url, file.data, false, true);
873
+ file.data = xhr.response;
874
+ this.cache.addSound(file.key, file.url, file.data);
875
+ if (file.autoDecode) {
876
+ this.game.sound.decode(file.key);
915
877
  }
916
878
  break;
917
879
  case 'text':
@@ -35,7 +35,6 @@ export default class {
35
35
  this.override = false;
36
36
  this.allowMultiple = false;
37
37
  this.usingWebAudio = this.game.sound.usingWebAudio;
38
- this.usingAudioTag = this.game.sound.usingAudioTag;
39
38
  this.externalNode = null;
40
39
  this.masterGainNode = null;
41
40
  this.gainNode = null;
@@ -54,16 +53,6 @@ export default class {
54
53
  if (connect) {
55
54
  this.gainNode.connect(this.masterGainNode);
56
55
  }
57
- } else if (this.usingAudioTag) {
58
- if (this.game.cache.getSound(key) && this.game.cache.isSoundReady(key)) {
59
- this._sound = this.game.cache.getSoundData(key);
60
- this.totalDuration = 0;
61
- if (this._sound.duration) {
62
- this.totalDuration = this._sound.duration;
63
- }
64
- } else {
65
- this.game.cache.onSoundUnlock.add(this.soundHasUnlocked, this);
66
- }
67
56
  }
68
57
  this.onDecoded = new Signal();
69
58
  this.onPlay = new Signal();
@@ -219,9 +208,6 @@ export default class {
219
208
  } else if (this.gainNode) {
220
209
  this._sound.disconnect(this.gainNode);
221
210
  }
222
- } else if (this.usingAudioTag) {
223
- this._sound.pause();
224
- this._sound.currentTime = 0;
225
211
  }
226
212
  this.isPlaying = false;
227
213
  }
@@ -411,9 +397,6 @@ export default class {
411
397
  } else if (this.gainNode) {
412
398
  this._sound.disconnect(this.gainNode);
413
399
  }
414
- } else if (this.usingAudioTag) {
415
- this._sound.pause();
416
- this._sound.currentTime = 0;
417
400
  }
418
401
  }
419
402
  this.pendingPlayback = false;
@@ -458,14 +441,6 @@ export default class {
458
441
  }
459
442
  }
460
443
 
461
- updateGlobalVolume(globalVolume) {
462
- // this._volume is the % of the global volume this sound should be played at
463
- if (this.usingAudioTag && this._sound) {
464
- this._sound.volume = globalVolume * this._volume;
465
- }
466
-
467
- }
468
-
469
444
  destroy(remove = true) {
470
445
  this._markedToDelete = true;
471
446
  this._removeFromSoundManager = remove;
@@ -502,15 +477,11 @@ export default class {
502
477
  this._muteVolume = this._tempVolume;
503
478
  if (this.usingWebAudio) {
504
479
  this.gainNode.gain.value = 0;
505
- } else if (this.usingAudioTag && this._sound) {
506
- this._sound.volume = 0;
507
480
  }
508
481
  } else {
509
482
  this._muted = false;
510
483
  if (this.usingWebAudio) {
511
484
  this.gainNode.gain.value = this._muteVolume;
512
- } else if (this.usingAudioTag && this._sound) {
513
- this._sound.volume = this._muteVolume;
514
485
  }
515
486
  }
516
487
  this.onMute.dispatch(this);
@@ -521,17 +492,14 @@ export default class {
521
492
  }
522
493
 
523
494
  set volume(value) {
524
- const normalizedValue = this.usingAudioTag ? Math.max(0, Math.min(1, value)) : value;
525
495
  if (this._muted) {
526
- this._muteVolume = normalizedValue;
496
+ this._muteVolume = value;
527
497
  return;
528
498
  }
529
- this._tempVolume = normalizedValue;
530
- this._volume = normalizedValue;
499
+ this._tempVolume = value;
500
+ this._volume = value;
531
501
  if (this.usingWebAudio) {
532
- this.gainNode.gain.value = normalizedValue;
533
- } else if (this.usingAudioTag && this._sound) {
534
- this._sound.volume = normalizedValue;
502
+ this.gainNode.gain.value = value;
535
503
  }
536
504
  }
537
505
 
@@ -18,7 +18,6 @@ export default class {
18
18
  this.context = null;
19
19
  this.baseLatency = 0; // https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/baseLatency
20
20
  this.usingWebAudio = false;
21
- this.usingAudioTag = false;
22
21
  this.noAudio = false;
23
22
  this.connectToMaster = true;
24
23
  this.touchLocked = false;
@@ -55,12 +54,7 @@ export default class {
55
54
  }
56
55
  }
57
56
  if (this.context === null || (this.context && this.context.createGain === undefined && this.context.createGainNode === undefined)) {
58
- // No Web Audio support - how about legacy Audio?
59
- if (window.Audio === undefined) {
60
- this.noAudio = true;
61
- return;
62
- }
63
- this.usingAudioTag = true;
57
+ this.noAudio = true;
64
58
  } else {
65
59
  this.usingWebAudio = true;
66
60
  this.baseLatency = this.context.baseLatency || (256 / (this.context.sampleRate || 44100));
@@ -71,16 +65,21 @@ export default class {
71
65
  }
72
66
  this.masterGain.gain.value = 1;
73
67
  this.masterGain.connect(this.context.destination);
74
- if (this.context.state === 'suspended') {
75
- this.game.input.onUp.addOnce(this.resumeWebAudio, this);
76
- }
77
68
  }
78
- if (!this.noAudio && (this.game.device.iOS || this.game.device.android)) {
69
+ if (this.usingWebAudio && (this.game.device.iOS || this.game.device.android)) {
79
70
  this.game.input.addTouchLockCallback(this.unlock, this, true);
80
71
  this.touchLocked = true;
81
72
  }
82
73
  }
83
74
 
75
+ checkContextState() {
76
+ if (this.usingWebAudio && this.context.state === 'suspended') {
77
+ this.game.input.onUp.addOnce(this.resumeWebAudio, this);
78
+ return true;
79
+ }
80
+ return false;
81
+ }
82
+
84
83
  resumeWebAudio() {
85
84
  if (this.usingWebAudio && this.context.state === 'suspended') {
86
85
  this.context.resume();
@@ -88,28 +87,21 @@ export default class {
88
87
  }
89
88
 
90
89
  unlock() {
91
- if (this.noAudio || !this.touchLocked || this._unlockSource !== null) {
90
+ if (!this.touchLocked || this._unlockSource !== null) {
92
91
  return true;
93
92
  }
94
- // Global override (mostly for Audio Tag testing)
95
- if (this.usingAudioTag) {
96
- this.touchLocked = false;
97
- this._unlockSource = null;
98
- } else if (this.usingWebAudio) {
99
- // Create empty buffer and play it
100
- // The SoundManager.update loop captures the state of it and then resets touchLocked to false
101
- const buffer = this.context.createBuffer(1, 1, 22050);
102
- this._unlockSource = this.context.createBufferSource();
103
- this._unlockSource.buffer = buffer;
104
- this._unlockSource.connect(this.context.destination);
105
- if (this._unlockSource.start === undefined) {
106
- this._unlockSource.noteOn(0);
107
- } else {
108
- this._unlockSource.start(0);
109
- }
110
- this.resumeWebAudio();
93
+ // Create empty buffer and play it
94
+ // The SoundManager.update loop captures the state of it and then resets touchLocked to false
95
+ const buffer = this.context.createBuffer(1, 1, 22050);
96
+ this._unlockSource = this.context.createBufferSource();
97
+ this._unlockSource.buffer = buffer;
98
+ this._unlockSource.connect(this.context.destination);
99
+ if (this._unlockSource.start === undefined) {
100
+ this._unlockSource.noteOn(0);
101
+ } else {
102
+ this._unlockSource.start(0);
111
103
  }
112
- // We can remove the event because we've done what we needed (started the unlock sound playing)
104
+ this.resumeWebAudio();
113
105
  return true;
114
106
  }
115
107
 
@@ -271,12 +263,6 @@ export default class {
271
263
  this._muteVolume = this.masterGain.gain.value;
272
264
  this.masterGain.gain.value = 0;
273
265
  }
274
- // Loop through sounds
275
- for (let i = 0; i < this._sounds.length; i += 1) {
276
- if (this._sounds[i].usingAudioTag) {
277
- this._sounds[i].mute = true;
278
- }
279
- }
280
266
  this.onMute.dispatch();
281
267
  }
282
268
 
@@ -288,12 +274,6 @@ export default class {
288
274
  if (this.usingWebAudio) {
289
275
  this.masterGain.gain.value = this._muteVolume;
290
276
  }
291
- // Loop through sounds
292
- for (let i = 0; i < this._sounds.length; i += 1) {
293
- if (this._sounds[i].usingAudioTag) {
294
- this._sounds[i].mute = false;
295
- }
296
- }
297
277
  this.onUnMute.dispatch();
298
278
  }
299
279
 
@@ -349,13 +329,6 @@ export default class {
349
329
  this._volume = value;
350
330
  if (this.usingWebAudio) {
351
331
  this.masterGain.gain.value = value;
352
- } else {
353
- // Loop through the sound cache and change the volume of all html audio tags
354
- for (let i = 0; i < this._sounds.length; i += 1) {
355
- if (this._sounds[i].usingAudioTag) {
356
- this._sounds[i].updateGlobalVolume(value);
357
- }
358
- }
359
332
  }
360
333
  this.onVolumeChange.dispatch(value);
361
334
  }