0
votes

I followed a simple tutorial to create a game using Phaser and I have modified all the files and directories to make it work with cordova so that it works on Android. The problem is the sound that plays on browser is not playing on phone. I read that I should use the cordova Media API so I added it but I get the error "Media is not defined" when running the game. This is the order in which I link to the js files in index.html:

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/phaser.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>

I'm creating the audio variable in the preload function of phaser like so:

preload: function() { 
    var audio = new Media('/android_asset/www/img/assets/jump.wav');
}

And in the jump function:

jump: function() {
    window.audio.play();
}

I can't understand what's the problem.

1
did you load it after device ready?Kenneth Li
Are you talking about cordova.js? I'm not using the deviceready event which is default in cordova because Phaser doesn't need it.Cress
but you use cordova api! any cordova Api can be used only after device readyKenneth Li
omg it's wav! did u try mp3! a month ago i've problem using wav then i change it to mp3Kenneth Li
It was the deviceready event that wasn't correctly set. Thank you for your help.Cress

1 Answers

0
votes

you need to integrate cordova plugin native sound with phaser js. 1. create a cordova project 2. add android platform 3. add plugin native sound

cordova plugin add cordova-plugin-nativeaudio

to use it: you also need to play start and stop loop sound to avoid error here is small code i capture for you:

declare all global variable and Phaser game state on index.html file '

var HomeState;
var LevelState;
    var GameOver;
    var GameWinner;
    var GameState;
    var CreditState;
    var ShareState;
    var SellState;

    var app;
    enter code here

create app  class object
create 3 main method for audio function

    playNativeSoundLoop:function(keyid)
    {
     window.plugins.NativeAudio.loop(keyid);

    },
    playNativeSound:function(keyid)
    {
     window.plugins.NativeAudio.play(keyid);        
    },
    stopNativeSound:function(keyid)
    {
     window.plugins.NativeAudio.stop(keyid);
    //window.plugins.NativeAudio.unload(keyid);  
    }
'

i can give your full code by download this sample game, that prove what i mention. This game made pure by phaser js and cordova

https://play.google.com/store/apps/details?id=com.s2team.blindmaze&hl=en