使用 JavaScript/HTML5 即時生成聲音
Web Audio API 用於控制音訊,允許您選擇音訊源。您還可以新增效果;建立音訊視覺化、全景聲等。
例項
您可以嘗試執行以下程式碼段生成聲音 −
// use one context per document. Here we are creating one context for one document. You can create for other documents also var context = new (window.AudioContext || window.webkitAudioContext)(); // oscillator var os = context.createOscillator(); os.type = 'sine'; // sine is the default. So you can also use square, saw tooth, triangle os.frequency.value = 500; // setting the frequency Hz os.connect(context.destination); // connecting to the destination // starting the oscillator os.start(); os.stop(context.currentTime + 5); // stop 5 seconds after the current time
廣告