[html5]html5音频API一个范例代码
[html5]html5音频API一个范例代码
html5音频API一个范例
- window.onload = init;
- var context;
- var bufferLoader;
- function init() {
- // Fix up prefixing
- windowwindow.AudioContext = window.AudioContext || window.webkitAudioContext;
- context = new AudioContext();
- bufferLoader = new BufferLoader(
- context,
- [
- '../sounds/hyper-reality/br-jam-loop.wav',
- '../sounds/hyper-reality/laughter.wav',
- ],
- finishedLoading
- );
- bufferLoader.load();
- }
- function finishedLoading(bufferList) {
- // Create two sources and play them both together.
- var source1 = context.createBufferSource();
- var source2 = context.createBufferSource();
- source1.buffer = bufferList[0];
- source2.buffer = bufferList[1];
- source1.connect(context.destination);
- source2.connect(context.destination);
- source1.start(0);
- source2.start(0);
- }
- for (var bar = 0; bar < 2; bar++) {
- var time = startTime + bar * 8 * eighthNoteTime;
- // Play the bass (kick) drum on beats 1, 5
- playSound(kick, time);
- playSound(kick, time + 4 * eighthNoteTime);
- // Play the snare drum on beats 3, 7
- playSound(snare, time + 2 * eighthNoteTime);
- playSound(snare, time + 6 * eighthNoteTime);
- // Play the hi-hat every eighth note.
- for (var i = 0; i < 8; ++i) {
- playSound(hihat, time + i * eighthNoteTime);
- }
- }
- var RhythmSample = {
- };
- RhythmSample.play = function() {
- function playSound(buffer, time) {
- var source = context.createBufferSource();
- source.buffer = buffer;
- source.connect(context.destination);
- if (!source.start)
- sourcesource.start = source.noteOn;
- source.start(time);
- }
- var kick = BUFFERS.kick;
- var snare = BUFFERS.snare;
- var hihat = BUFFERS.hihat;
- // We'll start playing the rhythm 100 milliseconds from "now"
- var startTime = context.currentTime + 0.100;
- var tempo = 80; // BPM (beats per minute)
- var eighthNoteTime = (60 / tempo) / 2;
- // Play 2 bars of the following:
- for (var bar = 0; bar < 2; bar++) {
- var time = startTime + bar * 8 * eighthNoteTime;
- // Play the bass (kick) drum on beats 1, 5
- playSound(kick, time);
- playSound(kick, time + 4 * eighthNoteTime);
- // Play the snare drum on beats 3, 7
- playSound(snare, time + 2 * eighthNoteTime);
- playSound(snare, time + 6 * eighthNoteTime);
- // Play the hi-hat every eighthh note.
- for (var i = 0; i < 8; ++i) {
- playSound(hihat, time + i * eighthNoteTime);
- }
- }
- };
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...