Solve Your Game Audio Problems on iOS and Android with Web Audio API

Solve Your Game Audio Problems on iOS and Android with Web Audio API

So you made your recreation or another app that has a number of sound results produced by a number of audio information. It works nice in your desktop, laptop computer, and many others. browsers however you then understand that you’ve got some main points with a number of sounds (and even single) on cellular and pill platforms like iOS and android browsers. Sad!

You attempt numerous strategies to repair it however no luck! Don’t fear, there’s a very good option to remedy it with – because the title says – Web Audio API.

What’s the one factor each developer desires? More screens! Enhance your coding expertise with an exterior monitor to extend display screen actual property.

Web Audio API

The HTML5 audio factor has some severe limitations making it just about unusable in numerous environments, particularly cellular browsers. We mentioned them in one of many earlier posts. This is without doubt one of the the reason why the Web Audio API has been launched, to work round these restrictions and do low stage processing and synthesizing of single or a number of audio streams.

HTML5 Rocks has a really stable introduction on this very matter together with some useful FAQs. Do learn them to get acquainted with this API.

Implementation

If you’ve learn that html5rocks tutorial, you then most likely already know what to do subsequent, though it could be good to share what I attempted in my latest recreation that labored like a attraction on iOS 6 browsers.

When the sport hundreds, we create an audio context and cargo all of the audio knowledge.

if (typeof webkitAudioContext !== 'undefined') {
  var audio_ctx = new webkitAudioContext();
}
  
perform loadMusic(url, cb) {
  var req = new XMLHttpRequest();
  req.open('GET', url, true);
  // XHR2
  req.responseType = 'arraybuffer';

  req.onload = perform() {
    audio_ctx.decodeAudioKnowledge(req.response, cb);
  };

  req.ship();
}

loadMusic is only a utility perform that hundreds the audio through XHR. Let’s have a look at how we invoke it.

GAME.audio = {};

var audio = {
  enemy: 'sfx/enemy.mp3',
  normal_jump: 'sfx/normal_jump.mp3',
  super_jump: 'sfx/super_jump.mp3',
  space_belt: 'sfx/space_belt.mp3',
  game_over: 'sfx/game_over.mp3'
}

var loadAudioKnowledge = perform(title, url) {

  // Async
  loadMusic(url, perform(buffer) {
    GAME.audio[name] = buffer;
  });

};

for (var title in audio) {
  var url = audio[name];
  loadAudioKnowledge(title, url);
}

We simply loaded all our audio knowledge and saved them in an object accessible on a worldwide variable known as GAME that acts as a namespace. So how can we play the sound precisely ? Let’s write a number of helper features that may assist us obtain this.

perform playSound (buffer, choose, cb) {
  if (!choose) cb = choose;
  choose = choose || {};

  var src = audio_ctx.createBufferSource();
  src.buffer = buffer;

  gain_node = audio_ctx.createGainNode();
  src.join(gain_node);
  
  gain_node.join(audio_ctx.vacation spot);
  //console.log(gain_node);

  if (typeof choose.sound !== 'undefined')
    gain_node.achieve.worth = choose.sound;
  else
    gain_node.achieve.worth = 1;

  // Options
  if (choose.loop)
    src.loop = true;

  src.noteOn(0);

  cb(src);
}

perform ceaseSound (src) {
  src.noteOff(0);
}

Do you discover the playSound technique has a callback parameter cb which is known as with the buffer supply object ? I’ll present you why I did that in a bit (principally in order that we are able to cease/pause the supply later). So how can we go about calling playSound to play our sound at completely different actions within the recreation ?

perform playGameSound(title, choose) {
  choose = choose || {};

  var cb = perform(src) {
    GAME.audio_src[name] = src;
  };

  playSound( GAME.audio[name], choose, cb );
}

// This his how we name it
playGameSound('enemy', {loop: true, sound: 0.5})

Take a detailed have a look at this piece of code GAME.audio_src[name] = src; – we’re doing this in order that we are able to cease the sound, every time we wish to, fairly simply. We retailer the supply buffer object within the audio_src object by the title and later after we wish to cease it, we simply name ceaseSound with the identical object.

ceaseSound(GAME.audio_src[name]);

Hopefully, you get the complete concept of utilizing the net audio API to play a number of audio in Webkit browsers now! This actually works nicely in iOS safari/chrome. But keep in mind, it’s only supported in iOS 6 (with safari 6), however not iOS 5. Supporting flawless audio playback in iOS 5 remains to be a ache. We merely determined to surrender on it.

Falling Back to HTML5 Audio

At the second, Web Audio API is simply supported in webkit browsers. So we have to gracefully fallback to html5 audio parts for others like Firefox, Opera and Internet Explorer 9+. Doing that is fairly easy, simply examine for Web Audio API help – one thing like typeof webkitAudioContext !== 'undefined' must be adequate for now – and when there’s no help begin creating audio parts and append them to the DOM.

for (var title in audio) {
  var url = audio[name];

  // Create an audio node
  var audio_el = doc.createElement('audio');

  // Source nodes for mp3 and ogg
  var src1_el = doc.createElement('supply');
  var src2_el = doc.createElement('supply');

  audio_el.id = title;

  src1_el.src = url;
  src1_el.sort = 'audio/mp3';

  src2_el.src = url.change('.mp3', '.ogg');
  src2_el.sort = 'audio/ogg';
  
  // Append OGG first (else firefox cries)
  audio_el.appendChild(src2_el);
  // Append MP3 second/subsequent
  audio_el.appendChild(src1_el);

  $$('physique').appendChild(audio_el);

  GAME.audio[name] = audio_el;
}

When it is advisable to play the sound, examine for net audio API help once more and when there’s no help, name play() to start out and pause() to cease the audio information (on GAME.audio[name]).

That’s just about it. Now you’ll be capable of begin enjoying audio flawlessly in iOS 6 mobiles and ipads atleast. Let’s hope that the audio help throughout all platforms will get rock stable quickly!

Recommended from our customers: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch.

Free Download

HI-FI News

through Code Theory https://codetheory.in

April 3, 2024 at 03:26PM