Created
May 6, 2014 15:37
-
-
Save jshbrntt/e9dd3d0802a57509eed5 to your computer and use it in GitHub Desktop.
Audio for Cordova Mobile Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jshint browser: true, devel: true */ | |
/* global require, module, plugins, Media */ | |
module.exports = { | |
debug: false, | |
sounds: {}, | |
clones: {}, | |
root: window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/')) + '/', | |
manifest: { | |
failure: 'assets/sounds/failure.mp3', | |
flip: 'assets/sounds/flip.mp3', | |
incorrect: 'assets/sounds/incorrect.mp3', | |
success: 'assets/sounds/success.mp3', | |
tap: 'assets/sounds/tap.mp3' | |
}, | |
initialize: function () { | |
this.parse(); | |
}, | |
parse: function () { | |
for (var id in this.manifest) { | |
if (this.debug) | |
console.log('[AUDIO]: Parsing: "' + id + '" [' + this.manifest[id] + ']'); | |
// Cordova LowLatencyAudio Plugin | |
if (typeof plugins !== 'undefined') { | |
console.log('[AUDIO]: Using Cordova LowLatencyAudio Plugin'); | |
plugins.LowLatencyAudio.preloadFX(id, this.manifest[id], this.parseSuccess, this.parseFail); | |
continue; | |
} | |
// Cordova Media Plugin | |
if (typeof Media !== 'undefined') { | |
console.log('[AUDIO]: Using Cordova Media Plugin'); | |
this.sounds[id] = new Media(this.root + this.manifest[id], this.parseSuccess, this.parseFail); | |
this.clones[id] = []; | |
continue; | |
} | |
// HTML5 Audio Element | |
if (typeof Audio !== 'undefined') { | |
console.log('[AUDIO]: Using HTML5 Audio Element'); | |
this.sounds[id] = new Audio(this.manifest[id]); | |
this.clones[id] = []; | |
continue; | |
} | |
} | |
}, | |
parseSuccess: function (e) { | |
if (this.debug) { | |
console.log('[AUDIO]: Parse Success!'); | |
console.log(e); | |
} | |
}, | |
parseFail: function (e) { | |
if (this.debug) { | |
console.log('[AUDIO]: Parse Fail!'); | |
console.log(e); | |
} | |
}, | |
play: function (id) { | |
if (this.debug) { | |
console.log('[AUDIO]: Playing: "' + id + '"'); | |
} | |
// Cordova LowLatencyAudio Plugin | |
if (typeof plugins !== 'undefined') { | |
plugins.LowLatencyAudio.play(id); | |
return; | |
} | |
var clones = this.clones[id]; | |
// Cordova Media Plugin | |
if (typeof Media !== 'undefined') { | |
this.sounds[id].play(); | |
var position; | |
this.sounds[id].getCurrentPosition(function (current) { | |
position = current; | |
}); | |
if (position <= 0) { | |
this.sounds[id].play(); | |
} else { | |
clones.push(new Media(this.sounds[id].src, null, null, function (mediaStatus) { | |
if (mediaStatus == Media.MEDIA_STOPPED) { | |
this.release(); | |
clones.splice(clones.indexOf(this), 1); | |
} | |
})); | |
clones[clones.length - 1].play(); | |
} | |
return; | |
} | |
// HTML5 Audio Element | |
if (typeof Audio !== 'undefined') { | |
var clone = this.sounds[id].cloneNode(); | |
clone.onended = function () { | |
clones.splice(clones.indexOf(this), 1); | |
}; | |
clones.push(clone); | |
clone.play(); | |
return; | |
} | |
}, | |
playSuccess: function (e) { | |
if (this.debug) { | |
console.log('[AUDIO]: Play Success!'); | |
console.log(e); | |
} | |
}, | |
playFail: function (e) { | |
if (this.debug) { | |
console.log('[AUDIO]: Play Fail!'); | |
console.log(e); | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment