Jump to content

Audio Problem Phonegap iOS 9


bymafmaf
 Share

Recommended Posts

I had many problems with audio working with Phaser and plain HTML5.  I finally used Cordova's Native Audio plugin, like this:

window.plugins.NativeAudio.preloadSimple( 'zap', 'asset/audio/zap.ogg', function(msg){}, function(msg){    console.log( 'error: ' + msg );});

To play the audio, I did this:

window.plugins.NativeAudio.play( 'zap' );

[Whoops -- I edited the play line  above. It had erroneously listed the non-Cordova play function.]

 

Tom

Edited by tsphillips
Link to comment
Share on other sites

Phaser wants to use WebAudio if available.  Some relevant snippets are below.  There was an interesting post on StackOverflow about WebAudio and iOS: http://stackoverflow.com/questions/12517000/no-sound-on-ios-6-web-audio-api  Apparently, the only way to have sounds start playing was (or is?) "to play a sound in a user input event (create a buffer source, connect it to destination, and call noteOn())."  The writer also states that iOS 9 changed from allowing audio to start in a touchstart event to only starting in a touchend event.

 

Various Phaser code about initializing sound is below.

 

src/system/Device.js checks for the audio support:

    function _checkAudio () {        device.audioData = !!(window['Audio']);        device.webAudio = !!(window['AudioContext'] || window['webkitAudioContext']);        var audioElement = document.createElement('audio');        var result = false;        try {            if (result = !!audioElement.canPlayType)            {                if (audioElement.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/, ''))                {                    device.ogg = true;                }                if (audioElement.canPlayType('audio/ogg; codecs="opus"').replace(/^no$/, '') || audioElement.canPlayType('audio/opus;').replace(/^no$/, ''))                {                    device.opus = true;                }                if (audioElement.canPlayType('audio/mpeg;').replace(/^no$/, ''))                {                    device.mp3 = true;                }                // Mimetypes accepted:                //   developer.mozilla.org/En/Media_formats_supported_by_the_audio_and_video_elements                //   bit.ly/iphoneoscodecs                if (audioElement.canPlayType('audio/wav; codecs="1"').replace(/^no$/, ''))                {                    device.wav = true;                }                if (audioElement.canPlayType('audio/x-m4a;') || audioElement.canPlayType('audio/aac;').replace(/^no$/, ''))                {                    device.m4a = true;                }                if (audioElement.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/, ''))                {                    device.webm = true;                }            }        } catch (e) {        }    }

In src/sound/SoundManager.js there is an interesting comment about the audio context:

/*** The Sound Manager is responsible for playing back audio via either the Legacy HTML Audio tag or via Web Audio if the browser supports it.* Note: On Firefox 25+ on Linux if you have media.gstreamer disabled in about:config then it cannot play back mp3 or m4a files.* The audio file type and the encoding of those files are extremely important. Not all browsers can play all audio formats.* There is a good guide to what's supported here: http://hpr.dogphilosophy.net/test/** If you are reloading a Phaser Game on a page that never properly refreshes (such as in an AngularJS project) then you will quickly run out* of AudioContext nodes. If this is the case create a global var called PhaserGlobal on the window object before creating the game. The active* AudioContext will then be saved to window.PhaserGlobal.audioContext when the Phaser game is destroyed, and re-used when it starts again.** Mobile warning: There are some mobile devices (certain iPad 2 and iPad Mini revisions) that cannot play 48000 Hz audio.* When they try to play the audio becomes extremely distorted and buzzes, eventually crashing the sound system.* The solution is to use a lower encoding rate such as 44100 Hz.** @class Phaser.SoundManager* @constructor* @param {Phaser.Game} game - Reference to the current game instance.*/Phaser.SoundManager = function (game) {

In the boot function of the SoundManger (src/sound/SoundManager.js), Phaser is looking for the audio context (only the WebAudio part is here):

        if (window['PhaserGlobal'] && window['PhaserGlobal'].audioContext)        {            this.context = window['PhaserGlobal'].audioContext;        }        else        {            if (!!window['AudioContext'])            {                try {                    this.context = new window['AudioContext']();                } catch (error) {                    this.context = null;                    this.usingWebAudio = false;                    this.touchLocked = false;                }            }            else if (!!window['webkitAudioContext'])            {                try {                    this.context = new window['webkitAudioContext']();                } catch (error) {                    this.context = null;                    this.usingWebAudio = false;                    this.touchLocked = false;                }            }        }

Tom

Link to comment
Share on other sites

  • 2 months later...
On 27/12/2015 at 9:53 AM, tsphillips said:

I had many problems with audio working with Phaser and plain HTML5.  I finally used Cordova's Native Audio plugin, like this:


window.plugins.NativeAudio.preloadSimple( 'zap', 'asset/audio/zap.ogg', function(msg){}, function(msg){    console.log( 'error: ' + msg );});

To play the audio, I did this:


window.plugins.NativeAudio.play( 'zap' );

[Whoops -- I edited the play line  above. It had erroneously listed the non-Cordova play function.]

 

Tom

By using intel xdk , it complains about window not being defined and so on .

I included cordova.js before phaser in index.html, but it isn't recognized apparently .

Any hint ?

[EDIT] Just checking this app -Audio Player- from intel xdk repository :

https://github.com/gomobile/sample-audio-player

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...