First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Gonzalo Alustiza

Controlling sound playback from JavaScript

First of all let’s create an external JavaScript library to store our code. Using external code libraries is a must if you want to avoid filling your HTML with lots of <SCRIPT> tags. Remember that a JavaScript library is a text file with a .js extension. In this case our library will be named ‘soundtown.js’.

The JavaScript libraries may be created using a simple text editor, or pick your favorite code editor to accomplish the task.

A big advantage of using external code libraries is that whenever you need to make a change to the code, no matter if it is an update or a correction, there is a single place to modify, which greatly eases the task and preserves your sanity. This doesn't mean that you don’t need to test your code after modifications, it means that modifications are easier and don’t involve editing lots of web pages.

Once the library is created you'll need to reference it from HTML so the functions and data that contains can be called from your HTML code. To reference an external code library use:

<script SRC="soundtown.js">

Remember that the text between quotes should contain the relative or absolute path to your library and the name of the .js file that contains your code.

If your code library is written in a language other that JavaScript i.e. VBScript, you can add LANGUAGE="VBScript" to the <SCRIPT> tag so the browser loads the VBScript interpreter. There is no need to use LANGUAGE="JavaScript" since the browser assumes all code to be JavaScript code as default. Remember that (in Internet Explorer) mixing JavaScript code and VBScript code leads to longer load times for your pages because the browser needs to load the VBScript interpreter which takes time and consumes memory. As a rule of thumb, stick to only one scripting language and it will go fine.

Also bear in mind that it is recommended to insert references like the shown above between <HEAD></HEAD> tags to ensure your code is available as soon as possible.

To control sound playback from JavaScript we will need two functions, one to play the sound and a second one to stop the sound, all the code needed to implement this will go inside the code library we just created.

To simplify things we will have the code to play and stop sounds inside the same function so we use a simple function call but, depending on the parameters sent to the function it will play or stop sounds.

» Level Intermediate

Added: 2001-02-20
Rating: 5.83 Votes: 30
(10 being the highest)
» Author
My name is Gonzalo Alustiza and I work as a chief developer at Soundtown Argentina. Soundtown is a ten year old music production company specializing in music for advertising and TV which started to develop sonification solutions for the Internet.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.