为什么这个声音在 webaudio api 中不断重新触发?
Why does this sound keeps retriggering itself in webaudio api?
我想用 webaudio 播放声音 api,但音频不断地重新触发自己,随机地叠加在一起,发出疯狂的声音。
我遵循了这个教程:http://fourthof5.com/audio-visualisation-with-the-web-audio-api
我不太理解整个代码,但我认为没有告诉声音在任何地方重新触发。
但是它每隔几秒播放一次。
有什么想法吗?
谢谢
代码:
/* Hoist some variables. */
var audio, context;
/* Try instantiating a new AudioContext, throw an error if it fails. */
try {
/* Setup an AudioContext. */
context = new AudioContext();
} catch(e) {
throw new Error('The Web Audio API is unavailable');
}
/* Define a `Sound` Class */
var Sound = {
/* Give the sound an element property initially undefined. */
element: undefined,
/* Define a class method of play which instantiates a new Media Element
* Source each time the file plays, once the file has completed disconnect
* and destroy the media element source. */
play: function() {
var sound = context.createMediaElementSource(this.element);
this.element.onended = function() {
sound.disconnect();
sound = null;
}
sound.connect(context.destination);
/* Call `play` on the MediaElement. */
this.element.play();
}
};
/* Create an async function which returns a promise of a playable audio element. */
function loadAudioElement(url) {
return new Promise(function(resolve, reject) {
var audio = new Audio();
audio.addEventListener('canplay', function() {
/* Resolve the promise, passing through the element. */
resolve(audio);
});
/* Reject the promise on an error. */
audio.addEventListener('error', reject);
audio.src = url;
});
}
/* Let's load our file. */
loadAudioElement('/audio/sound.wav').then(function(elem) {
/* Instantiate the Sound class into our hoisted variable. */
audio = Object.create(Sound);
/* Set the element of `audio` to our MediaElement. */
audio.element = elem;
/* Immediately play the file. */
audio.play();
}, function(elem) {
/* Let's throw an the error from the MediaElement if it fails. */
throw elem.error;
});
上面的代码实际上工作正常,我的问题是我使用中间人和链轮,我调用了两次脚本。
一次使用链轮,一次手动 layout.erb
我想用 webaudio 播放声音 api,但音频不断地重新触发自己,随机地叠加在一起,发出疯狂的声音。
我遵循了这个教程:http://fourthof5.com/audio-visualisation-with-the-web-audio-api
我不太理解整个代码,但我认为没有告诉声音在任何地方重新触发。
但是它每隔几秒播放一次。
有什么想法吗? 谢谢
代码:
/* Hoist some variables. */
var audio, context;
/* Try instantiating a new AudioContext, throw an error if it fails. */
try {
/* Setup an AudioContext. */
context = new AudioContext();
} catch(e) {
throw new Error('The Web Audio API is unavailable');
}
/* Define a `Sound` Class */
var Sound = {
/* Give the sound an element property initially undefined. */
element: undefined,
/* Define a class method of play which instantiates a new Media Element
* Source each time the file plays, once the file has completed disconnect
* and destroy the media element source. */
play: function() {
var sound = context.createMediaElementSource(this.element);
this.element.onended = function() {
sound.disconnect();
sound = null;
}
sound.connect(context.destination);
/* Call `play` on the MediaElement. */
this.element.play();
}
};
/* Create an async function which returns a promise of a playable audio element. */
function loadAudioElement(url) {
return new Promise(function(resolve, reject) {
var audio = new Audio();
audio.addEventListener('canplay', function() {
/* Resolve the promise, passing through the element. */
resolve(audio);
});
/* Reject the promise on an error. */
audio.addEventListener('error', reject);
audio.src = url;
});
}
/* Let's load our file. */
loadAudioElement('/audio/sound.wav').then(function(elem) {
/* Instantiate the Sound class into our hoisted variable. */
audio = Object.create(Sound);
/* Set the element of `audio` to our MediaElement. */
audio.element = elem;
/* Immediately play the file. */
audio.play();
}, function(elem) {
/* Let's throw an the error from the MediaElement if it fails. */
throw elem.error;
});
上面的代码实际上工作正常,我的问题是我使用中间人和链轮,我调用了两次脚本。
一次使用链轮,一次手动 layout.erb