- window.AudioContext = window.AudioContext || window.webkitAudioContext;
- var audioCtx = new AudioContext();
- var oscillator = audioCtx.createOscillator();
- var gainNode = audioCtx.createGain();
- oscillator.connect(gainNode);
- gainNode.connect(audioCtx.destination);
- oscillator.type = 'sine';
- oscillator.frequency.value = 349.23;
- gainNode.gain.setValueAtTime(0, audioCtx.currentTime);
- gainNode.gain.linearRampToValueAtTime(1, audioCtx.currentTime + 0.01);
- oscillator.start(audioCtx.currentTime);
- gainNode.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 1);
- oscillator.stop(audioCtx.currentTime + 1);
复制代码 如果只是用来播放警示音的话,你把播放声音的代码替换成这个不需要资源文件的方式试试看能不能成?
|