简体   繁体   中英

Sound not playing with cordova and Phaser

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM