First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
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: 6 Votes: 29
(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!

Comments

  • 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: S.Saipriya
» Title: Flash Fire Effect
» Description: This tutorial explains about how to create Fire Effect in Flash of Actionscript 2.0.I hope this tutorial helps you!
Featured Sound Loops
Image for equalize

Audio Player

» Author: Aidan Crouzet-Pascal
» Title: Falling Star
» Description: This loop is a cool sounding, E minor loop.
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: Brandan Paulin
» Description: Epic sounding loop with Brass, Percussion, Strings and Winds. This is actually part of a Loop Pack called the Coming where you can download the rest of the loops that go with it FREE from my website.