Skip to content

Instantly share code, notes, and snippets.

@fcingolani
Last active January 19, 2018 04:29
Show Gist options
  • Save fcingolani/9176aed13701f9a0f55d to your computer and use it in GitHub Desktop.
Save fcingolani/9176aed13701f9a0f55d to your computer and use it in GitHub Desktop.
Audio support for Phaser 2.3.0 on Apache Cordova / PhoneGap
  1. Add the [https://github.com/floatinghotpot/cordova-plugin-nativeaudio](Cordova Native Audio Plugin) to your project:

     cordova plugin add cordova-plugin-nativeaudio
    
  2. Include sound-patch.js in your HTML, or just paste the code wherever you want it.

This doesn't support looping or volume. Modify the code to use NativeAudio.preloadComplex accordingly.

Phaser.SoundManager.prototype._play = Phaser.SoundManager.prototype.play;
Phaser.SoundManager.prototype.play = function (key, volume, loop) {
if(this.game.device.cordova){
window.plugins.NativeAudio.play(key);
}else{
return this._play(key, volume, loop);
}
}
Phaser.Loader.prototype._loadAudioTag = Phaser.Loader.prototype.loadAudioTag;
Phaser.Loader.prototype.loadAudioTag = function (file) {
if(this.game.device.cordova){
var _this = this;
window.plugins.NativeAudio.preloadSimple( file.key, this.transformUrl(file.url, file), function(){
_this.fileComplete(file);
}, function(){
_this.fileError(file);
});
}else{
this._loadAudioTag(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment