google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank
Guest, register






JavaScript Sound Playing with HTML5 If you are a big fan of new technologies on web development, then HTML should not be strange, because it seems that everybody talk about HTML5 everyday in meetings, conferences, product introductions, ...

Really HTML5 is a big improvement for web developers in tasks. We can do a lot of interesting things in HTML5 without any plug-in installation, such as playing HTML5 music/video, making HTML5 canvas, creating awesome HTML5 games, ...

And today, I would like to help you create a simple HTML5 audio player in this JavaScript article tutorial. With a few JavaScript codelines, we're able to play HTML5 audio JavaScript sounds, that's so cool!


Free iPage Web Hosting for First Year NOW



If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Try iPage for FREE First Year NOW

Generating sound with JavaScript in Firefox4 is very easy with Mozilla's Audio Data API. No plug-ins are required! In seven steps, and with only eight lines of code, this quick tutorial will show you how to create and play your own tones with JavaScript in Firefox 4.

2 Min. Tutorial: Generate sound in Firefox 4 with JavaScript

This tutorial will include:

  1. Create a basic audio setup in JavaScript
  2. Create and fill a buffer with audio samples
  3. Push the samples to the browser's sound engine

Things you will need:

  1. A copy of Firefox4, or the Firefox4 Beta.
  2. A blank document named "audio.html".
  3. A text editor.

Step 1: Create an HTML document

<script type="text/javascript">

  //write your code here
</script>
 
<button>Play</button>

Create your HTML document and add script-tags. Between the script tags, you can begin writting your JavaScript code. Then create a button element which we will use to play the sound.

Step 2: Create an Audio interface

Between your script tags, add the following line:

  var output = new Audio();

This first JavaScript command creates a new Audio interface in JavaScript called "output". We will use this interface to set up and play sound in the browser.

Step 3: Set up your audio element

  var output = new Audio();
 
  output.mozSetup( 1, 44100 );

This step uses the .mozSetup( channels, sampleRate ) method to switch the mode of the audio interface. Here we tell the browser that "output" should have 2 channels at 44.1 Khz. The two channels are left & right, and 44.1Khz is the same sample rate used by music CDs.

Step 4: Create a sample buffer

  var output = new Audio();

 
  output.mozSetup( 1, 44100 );
 
  var samples = new Float32Array( 22050 );

In Step 4 we create a sample buffer by adding a new WebGL Float32Array in JavaScript. The length of this array is the same as the audio sample rate. A "second's-worth" of mono audio at 44.1Khz, requires an array of 22050 indices.

Step 5: Filling the sample buffer

  var output = new Audio();

 
  output.mozSetup( 1, 44100 );  
 
  var samples = new Float32Array( 22050 );

 
  for(var i=0, l=samples.length; i< l; i++){

    samples[i] = Math.sin( i / 20 );

  }

Here we create a basic for-loop and fill the sample buffer array with a sine wave using the JavaScript Math object. The actual sound, or "waveform" is being generated in this loop with the Sin method, filling the buffer array with samples ranging from 1 to -1.

Step 6: Write the sample buffer out

<script type="text/javascript">
  var output = new Audio();

 
  output.mozSetup( 1, 44100 );
 
  var samples = new Float32Array( 22050 );
 
  for(var i=0, l=samples.length; i< l; i++){
    samples[i] = Math.sin( i / 20 );

  }
</script>
 
<button onclick="output.mozWriteAudio( samples );">Play</button>

In this final step, we use the .mozWriteAudio( sample_array ) method to play the audio. The mozWriteAudio method takes an array of Numbers as an argument and writes adds them to the sound engine of the browser. The browser then uses the operating system's drivers to play the sound over your computer speakers.

Normally you would attach this at an event listener, but for the sake of simplicity, we will add the code inline to the "onclick" event of the button element.

Step 7: Run the code

Your final code should look something like this:

<script type="text/javascript">

  // Create an Audio interface
  var output = new Audio();

  // Set up a mono channel at 44.1Khz
  output.mozSetup( 1, 44100 );

  // Create a sample buffer array
  var samples = new Float32Array( 22050 );

  // Fill the sample buffer array with values
  for(var i=0, l=samples.length; i< l; i++){
    samples[i] = Math.sin( i / 20 );
  }
   
</script>

<!-- Play the audio out -->
<button onclick="output.mozWriteAudio( samples );">Play</button>

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web