Building a Simple Desktop Sound Engine
Before delving into the contortions necessary to get audio playing on a mobile device, it’s worth taking a look at what’s involved in playing sound effects via the <audio> tag on the desktop.
Using Audio Tags for Game Audio
One of the problems with Audio objects from a game perspective is that each object can play only one sound at a time. This means that if you want to play the same sound effect twice at almost the exact same time (because, for example, two missiles just blew up) you can’t if you use a single audio element.
As a workaround to this limitation, HTML5 game developers quickly discovered that after a sound had been loaded, if you assigned its source to another, different audio element, the sound effect wouldn’t be downloaded again but would start playing almost immediately. This led to a design of game audio systems that had a number of pre-created Audio objects that were used as channels to play sound effects.
To make this work, it would be the audio system’s job to keep track of which of the channels were still playing audio and add any new sound effects only to channels that weren’t in the middle of playing.
Adding a Simple Sound System
In Chapter 10 the asset loader code has some functionality for loading sound files based on supported formats. This means that the loading side of things is already built. The only piece of code needed is setting up the channels and playing back audio where appropriate.
This example adds sound ...
Get Professional HTML5 Mobile Game Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.