Added ability for full Audio tag playback and iOS touch lock load support.

This commit is contained in:
Richard Davey
2013-07-16 23:32:47 +01:00
parent 72eb22128f
commit a2c756e37f
11 changed files with 620 additions and 215 deletions
+2
View File
@@ -639,6 +639,7 @@ module Phaser {
if (value == true && this._paused == false)
{
this._paused = true;
this.sound.pauseAll();
this._raf.callback = this.pausedLoop;
}
else if (value == false && this._paused == true)
@@ -646,6 +647,7 @@ module Phaser {
this._paused = false;
//this.time.time = window.performance.now ? (performance.now() + performance.timing.navigationStart) : Date.now();
this.input.reset();
this.sound.resumeAll();
if (this.isRunning == false)
{
+10 -7
View File
@@ -213,24 +213,27 @@ module Phaser {
*/
private visibilityChange(event) {
if (this.disablePauseScreen)
{
return;
}
if (event.type == 'pagehide' || event.type == 'blur' || document['hidden'] == true || document['webkitHidden'] == true)
{
if (this._game.paused == false)
if (this._game.paused == false && this.disablePauseScreen == false)
{
this.pauseGame();
}
else
{
this._game.paused = true;
}
}
else
{
if (this._game.paused == true)
if (this._game.paused == true && this.disablePauseScreen == false)
{
this.resumeGame();
}
else
{
this._game.paused = false;
}
}
}
+46 -2
View File
@@ -124,15 +124,43 @@ module Phaser {
*/
public addSound(key: string, url: string, data, webAudio: bool = true, audioTag: bool = false) {
console.log('Cache addSound', key, url, webAudio, audioTag);
console.log('Cache addSound: ' + key + ' url: ' + url, webAudio, audioTag);
var locked: bool = this._game.sound.touchLocked;
var decoded: bool = false;
if (audioTag) {
decoded = true;
}
this._sounds[key] = { url: url, data: data, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag };
this._sounds[key] = { url: url, data: data, locked: locked, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag };
}
public reloadSound(key: string) {
console.log('reloadSound', key);
if (this._sounds[key])
{
this._sounds[key].data.src = this._sounds[key].url;
this._sounds[key].data.addEventListener('canplaythrough', () => this.reloadSoundComplete(key), false);
this._sounds[key].data.load();
}
}
public onSoundUnlock: Phaser.Signal = new Phaser.Signal;
public reloadSoundComplete(key: string) {
console.log('reloadSoundComplete', key);
if (this._sounds[key])
{
this._sounds[key].locked = false;
this.onSoundUnlock.dispatch(key);
}
}
@@ -265,6 +293,22 @@ module Phaser {
}
/**
* Check whether an asset is decoded sound.
* @param key Asset key of the sound you want.
* @return {object} The sound data you want.
*/
public isSoundReady(key: string): bool {
if (this._sounds[key] && this._sounds[key].decoded == true && this._sounds[key].locked == false)
{
return true;
}
return false;
}
/**
* Check whether an asset is sprite sheet.
* @param key Asset key of the sprite sheet you want.
+24 -7
View File
@@ -330,6 +330,8 @@ module Phaser {
case 'audio':
file.url = this.getAudioURL(file.url);
console.log('Loader audio');
console.log(file.url);
if (file.url !== null)
{
@@ -344,13 +346,27 @@ module Phaser {
}
else if (this._game.sound.usingAudioTag)
{
file.data = new Audio();
file.data.name = file.key;
file.data.onerror = () => this.fileError(file.key);
file.data.preload = 'auto';
file.data.src = file.url;
file.data.addEventListener('canplaythrough', () => this.fileComplete(file.key), false);
file.data.load();
if (this._game.sound.touchLocked)
{
// If audio is locked we can't do this yet, so need to queue this load request somehow. Bum.
console.log('Audio is touch locked');
file.data = new Audio();
file.data.name = file.key;
file.data.preload = 'auto';
file.data.src = file.url;
this.fileComplete(file.key);
}
else
{
console.log('Audio not touch locked');
file.data = new Audio();
file.data.name = file.key;
file.data.onerror = () => this.fileError(file.key);
file.data.preload = 'auto';
file.data.src = file.url;
file.data.addEventListener('canplaythrough', () => this.fileComplete(file.key), false);
file.data.load();
}
}
}
@@ -379,6 +395,7 @@ module Phaser {
if (this._game.device.canPlayAudio(extension))
{
console.log('getAudioURL', urls[i]);
console.log(urls[i]);
return urls[i];
}
+71 -27
View File
@@ -43,8 +43,15 @@ module Phaser {
}
else
{
this._sound = this.game.cache.getSoundData(key);
this.totalDuration = this._sound.duration;
if (this.game.cache.getSound(key).locked == false)
{
this._sound = this.game.cache.getSoundData(key);
this.totalDuration = this._sound.duration;
}
else
{
this.game.cache.onSoundUnlock.add(this.soundHasUnlocked, this);
}
}
this._volume = volume;
@@ -61,6 +68,17 @@ module Phaser {
}
private soundHasUnlocked(key:string) {
if (key == this.key)
{
this._sound = this.game.cache.getSoundData(this.key);
this.totalDuration = this._sound.duration;
console.log('sound has unlocked', this._sound);
}
}
/**
* Local reference to the current Phaser.Game.
*/
@@ -151,8 +169,9 @@ module Phaser {
public update() {
if (this.pendingPlayback && this.game.cache.isSoundDecoded(this.key))
if (this.pendingPlayback && this.game.cache.isSoundReady(this.key))
{
console.log('pending over');
this.pendingPlayback = false;
this.play(this._tempMarker, this._tempPosition, this._tempVolume, this._tempLoop);
}
@@ -213,19 +232,24 @@ module Phaser {
if (marker !== '' && this.markers[marker])
{
this.loop = this.markers[marker].loop;
this.volume = this.markers[marker].volume;
this.position = this.markers[marker].start;
this.volume = this.markers[marker].volume;
this.loop = this.markers[marker].loop;
this.duration = this.markers[marker].duration * 1000;
}
else
{
this.loop = loop;
this.volume = volume;
this.position = position;
this.volume = volume;
this.loop = loop;
this.duration = 0;
}
this._tempMarker = marker;
this._tempPosition = position;
this._tempVolume = volume;
this._tempLoop = loop;
if (this.usingWebAudio)
{
// Does the sound need decoding?
@@ -267,10 +291,6 @@ module Phaser {
}
else
{
this._tempVolume = volume;
this._tempLoop = loop;
this._tempPosition = position;
this._tempMarker = marker;
this.pendingPlayback = true;
if (this.game.cache.getSound(this.key).isDecoding == false)
@@ -281,19 +301,42 @@ module Phaser {
}
else
{
if (this._sound.readyState == 4)
{
this._sound.currentTime = this.position;
this._sound.muted = this._muted;
this._sound.volume = this._volume;
this._sound.play();
console.log('Sound play Audio');
this.isPlaying = true;
this.startTime = this.game.time.now;
this.currentTime = 0;
this.stopTime = this.startTime + this.duration;
this.onPlay.dispatch(this);
if (this.game.cache.getSound(this.key).locked)
{
console.log('tried playing locked sound, pending set, reload started');
this.game.cache.reloadSound(this.key);
this.pendingPlayback = true;
}
else
{
console.log('sound not locked, state?', this._sound.readyState);
if (this._sound && this._sound.readyState == 4)
{
if (this.duration == 0)
{
this.duration = this.totalDuration * 1000;
}
console.log('playing', this._sound);
this._sound.currentTime = this.position;
this._sound.muted = this._muted;
this._sound.volume = this._volume;
this._sound.play();
this.isPlaying = true;
this.startTime = this.game.time.now;
this.currentTime = 0;
this.stopTime = this.startTime + this.duration;
this.onPlay.dispatch(this);
}
else
{
this.pendingPlayback = true;
}
}
}
}
@@ -316,7 +359,8 @@ module Phaser {
public resume() {
if (this.isPlaying == false && this._sound)
//if (this.isPlaying == false && this._sound)
if (this.paused && this._sound)
{
if (this.usingWebAudio)
{
@@ -392,8 +436,8 @@ module Phaser {
this._muteVolume = this.gainNode.gain.value;
this.gainNode.gain.value = 0;
}
else if (this.usingAudioTag)
{
else if (this.usingAudioTag && this._sound)
{
this._muteVolume = this._sound.volume;
this._sound.volume = 0;
}
@@ -406,7 +450,7 @@ module Phaser {
{
this.gainNode.gain.value = this._muteVolume;
}
else if (this.usingAudioTag)
else if (this.usingAudioTag && this._sound)
{
this._sound.volume = this._muteVolume;
}
@@ -424,7 +468,7 @@ module Phaser {
{
this.gainNode.gain.value = value;
}
else if (this.usingAudioTag)
else if (this.usingAudioTag && this._sound)
{
this._sound.volume = value;
}
+85 -27
View File
@@ -28,6 +28,21 @@ module Phaser {
this.channels = 1;
}
if (this.game.device.iOS || (window['PhaserGlobal'] && window['PhaserGlobal'].fakeiOSTouchLock))
{
console.log('iOS Touch Locked');
this.game.input.touch.callbackContext = this;
this.game.input.touch.touchStartCallback = this.unlock;
this.game.input.mouse.callbackContext = this;
this.game.input.mouse.mouseDownCallback = this.unlock;
this.touchLocked = true;
}
else
{
// What about iOS5?
this.touchLocked = false;
}
if (window['PhaserGlobal'])
{
// Check to see if all audio playback is disabled (i.e. handled by a 3rd party class)
@@ -48,17 +63,6 @@ module Phaser {
}
}
if (this.game.device.iOS && this.game.device.webAudio)
{
this.game.input.touch.callbackContext = this;
this.game.input.touch.touchStartCallback = this.unlock;
this.touchLocked = true;
}
else
{
// What about iOS5?
this.touchLocked = false;
}
this.usingWebAudio = true;
this.noAudio = false;
@@ -137,19 +141,34 @@ module Phaser {
public unlock() {
if (this.touchLocked == false || this.game.device.iOS == false)
if (this.touchLocked == false)
{
return;
}
// Create Audio tag?
console.log('SoundManager touch unlocked');
// Create empty buffer and play it
var buffer = this.context.createBuffer(1, 1, 22050);
this._unlockSource = this.context.createBufferSource();
this._unlockSource.buffer = buffer;
this._unlockSource.connect(this.context.destination);
this._unlockSource.noteOn(0);
if (this.game.device.webAudio && (window['PhaserGlobal'] && window['PhaserGlobal'].disableWebAudio == false))
{
console.log('create empty buffer');
// Create empty buffer and play it
var buffer = this.context.createBuffer(1, 1, 22050);
this._unlockSource = this.context.createBufferSource();
this._unlockSource.buffer = buffer;
this._unlockSource.connect(this.context.destination);
this._unlockSource.noteOn(0);
}
else
{
// Create an Audio tag?
console.log('create audio tag');
this.touchLocked = false;
this._unlockSource = null;
this.game.input.touch.callbackContext = null;
this.game.input.touch.touchStartCallback = null;
this.game.input.mouse.callbackContext = null;
this.game.input.mouse.mouseDownCallback = null;
}
}
@@ -180,7 +199,7 @@ module Phaser {
// Loop through sounds
for (var i = 0; i < this._sounds.length; i++)
{
if (this._sounds[i].usingAudioTag)
if (this._sounds[i])
{
this._sounds[i].mute = true;
}
@@ -203,7 +222,7 @@ module Phaser {
// Loop through sounds
for (var i = 0; i < this._sounds.length; i++)
{
if (this._sounds[i].usingAudioTag)
if (this._sounds[i])
{
this._sounds[i].mute = false;
}
@@ -250,6 +269,42 @@ module Phaser {
}
public stopAll() {
for (var i = 0; i < this._sounds.length; i++)
{
if (this._sounds[i])
{
this._sounds[i].stop();
}
}
}
public pauseAll() {
for (var i = 0; i < this._sounds.length; i++)
{
if (this._sounds[i])
{
this._sounds[i].pause();
}
}
}
public resumeAll() {
for (var i = 0; i < this._sounds.length; i++)
{
if (this._sounds[i])
{
this._sounds[i].resume();
}
}
}
/**
* Decode a sound with its assets key.
* @param key {string} Assets key of the sound to be decoded.
@@ -285,14 +340,17 @@ module Phaser {
public update() {
if (this.touchLocked && this._unlockSource !== null)
if (this.touchLocked)
{
if ((this._unlockSource.playbackState === this._unlockSource.PLAYING_STATE || this._unlockSource.playbackState === this._unlockSource.FINISHED_STATE))
if (this.game.device.webAudio && this._unlockSource !== null)
{
this.touchLocked = false;
this._unlockSource = null;
this.game.input.touch.callbackContext = null;
this.game.input.touch.touchStartCallback = null;
if ((this._unlockSource.playbackState === this._unlockSource.PLAYING_STATE || this._unlockSource.playbackState === this._unlockSource.FINISHED_STATE))
{
this.touchLocked = false;
this._unlockSource = null;
this.game.input.touch.callbackContext = null;
this.game.input.touch.touchStartCallback = null;
}
}
}