google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Phát âm thanh trên web với HTML5 Nếu bạn là người quan tâm đến các công nghệ mới trong lĩnh vực phát triển web thì hẳn nhiên sẽ biết đến các ưu điểm của HTML5; bởi hầu như mọi người đều nói về nó mỗi ngày, trong các cuộc gặp gỡ, hội thảo cũng như các buổi giới thiệu sản phẩm mới.

Thật sự thì HTML5 là một bước tiến lớn dành cho các nhà phát triển web trong công việc. Chúng ta có thể làm rất nhiều điều thú vị với HTML5 mà không cần bất kì một cài đặt bổ sung nào. chẳng hạn như chơi nhạc/video, vẽ vời, làm trò chơi, ...

Và hôm nay trong bài viết hướng dẫn JavaScript này, tôi muốn giới thiệu đến bạn một cách thức cực kì đơn giản và tuyệt vời để tạo một trình phát âm thanh trên web với HTML5, chỉ với một vài dòng mã JavaScript cực kì đơn giản, vui lòng vào trang chi tiết để xem thêm.


Nhãn: phát âm thanh, HTML5, phát triển web, chơi nhạc, vẽ vời, trình phát âm thanh

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

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>
iPhoneKer.com
Save up to 630$ when buy new iPhone 15

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

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

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

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web