Click here to Skip to main content
12,893,816 members (54,127 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 25 Sep 2012

Add music to a simple Windows 8 Store App

, 25 Sep 2012 Ms-PL
Rate this:
Please Sign up or sign in to vote.
This is a really simple implementation of JavaScript and HTML5 to be used in a Windows 8 Store app.


This article walks you through the process of adding music to your HTML5/JavaScript project, and that is all that the article shows you.


In many articles the code has classes and code that the developer is used to utilizing. This article is written to guide you in how to add music to an HTML5 program that could be added to the Windows 8 Store, with a lot more work.  It has one sound file and several lines of code that are overly commented.

For more information, see my blog at:

Using the code

Using the Audio object in the HTML5 DOM, you get an easy way to add music and sounds to your JavaScript based Windows 8 Store game or project.


But to get started you have to add a single line to your HTML5 default.html page:

<canvas id="canvas">

The word between the quotes can be whatever you want to name the canvas, and frankly it was likely a bad idea to use the word: canvas.  But many examples do so, and I just followed the practice.


These two variables are generated to hold the canvas object and the context object.

  • var canvas;
  • var ctx;

Information about the HTML5 DOM

The Audio HTML5 tag (new to HTML5) information is included in the DOM single page (a long single page) document that can be found at:

In the case of the JavaScript code, the object is constructed using the code:

var musicGame = new Audio("filepath to sound");

Then the music uses one of the attributes on the object to loop the music:

musicGame.loop = true;
    <meta charset="utf-8" />

    <link href="" rel="stylesheet";/>
    <script src=""></script>
    <script src=""></script>
    <link href="" rel="stylesheet" />
    <script src=""></script>
Content Goes Here

<canvas id="canvas">
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    //** You add these variables canvas and context
    var canvas;
    var ctx;

    //** You add the variable musicGame, the            *****
    //** Audio component is part of the SoundJS library *****
    //** and assists you with your project building     *****
    var musicGame = new Audio("/sounds/hydrogen.mp3");
    musicGame.loop = true;

    //** You add the following line to fire the        ******
    //** function initialize (scroll down to see)      ******
    //** This will cause the function initialize       ******
    //** to fire when your DOM is loaded               ******
    document.addEventListener("DOMContentLoaded", initialize, false);

    //***** You add the following function              ****
    //***** Where you place the function as long as you ****
    //***** put it above the })(); line                 ****
    function initialize() {
        //**** Initialize the Canvas                    ****
        //**** You add the following code               ****
        canvas = document.getElementById("canvas");
        //**** You add the following code
        //**** if you do not add                        ****
        //**** <canvas id="canvas"/>                    ****
        //**** to the default.html then you will        ****
        //**** get an error here                        ****
        ctx = canvas.getContext("2d");
        //**** Add the following code                   ****
        //**** The play is part of the soundjs.js       ****
        //**** library                                  ****;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().



Initial version.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

sam stokes
Instructor / Trainer Microsoft
United States United States
Sam Stokes works for Microsoft as a technology evangelist and is focused on working with colleges, students and professors.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 25 Sep 2012
Article Copyright 2012 by sam stokes
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid