Playing the Sound

Finally, we get to the point where we can play a sound inside our canvasApp() function. Because we preloaded the sound originally outside the context of this function into a variable with a global scope, we just need to call the play() function audioElement to start playing the sound:

audioElement.play();

Figure 7-4 shows what this Canvas application will look like when executed in an HTML5-compliant web browser. (Notice that we have left the display of the audio properties in this application.)

Sound loaded and played “on” the canvas

Figure 7-4. Sound loaded and played “on” the canvas

Get HTML5 Canvas, 2nd Edition 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.