google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






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>
AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web