google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






JavaScript Sound Jouer avec HTML5 Si vous


Étiquette: toile HTML5, clip de HTML5, HTML5 audio JavaScript, HTML5 lecteur audio, JavaScript joueur de son, son jeu HTML, HTML5

Gratuit iPage hébergement Web pour la première année MOMENT



Si vous êtes toujours à la recherche d'un fournisseur d'hébergement Web fiable avec des tarifs abordables, pourquoi vous ne prenez pas un peu de temps pour essayer iPage, seulement avec $1.89/month, inclus $500+ Crédits supplémentaires gratuites pour le paiement de 24 mois ($45)?

Plus de 1.000.000 de clients + existisng peuvent pas avoir tort, vraiment vous n'êtes pas aussi! Plus important encore, lorsque vous enregistrez l'hébergement web à iPage grâce à notre lien, nous allons être heureux de renvoyer un plein remboursement. C'est génial! Vous devriez essayer iPage hébergement web GRATUITEMENT maintenant! Et contactez-nous pour tout ce que vous devez savoir sur iPage.
Essayez iPage GRATUIT première année MOMENT

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 par jour


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web