admin管理员组文章数量:1336613
I'm building a ponent that shows information about a video's audio. I use the AudioContext interface to get audio samples from a HTML5 video element. It works fine the first time I create the ponent, but when the ponent is unmounted and then recreated at a later point, I get the following error message:
Uncaught InvalidStateError: Failed to execute 'createMediaElementSource' on 'AudioContext': HTMLMediaElement already connected previously to a different MediaElementSourceNode.
Here's how I get the audio:
const video = document.querySelectorAll('video')[0]
if (!window.audioContext) {
window.audioContext = new (window.AudioContext || window.webkitAudioContext)
}
if (!this.source && !this.scriptNode) {
this.source = window.audioContext.createMediaElementSource(video)
this.scriptNode = window.audioContext.createScriptProcessor(4096, 1, 1)
}
this.scriptNode.onaudioprocess = (evt) => {
// Processing audio works fine...
}
this.source.connect(this.scriptNode)
this.scriptNode.connect(window.audioContext.destination)
And when the ponent is unmounted I do:
if (this.source && this.scriptNode) {
this.source.disconnect(this.scriptNode)
this.scriptNode.disconnect(window.audioContext.destination)
}
I thought this would put me in a state where I can safely create and connect new nodes. But the next time the ponent is mounted, this block throws the error mentioned earlier:
if (!this.source && !this.scriptNode) {
this.source = window.audioContext.createMediaElementSource(video) // this throws the error
this.scriptNode = window.audioContext.createScriptProcessor(4096, 1, 1)
}
I could get it to work by making everything global, i.e. putting source
and scriptNode
on window
rather than this
. But that won't work if my video element changes. What's the correct way to do this?
I'm building a ponent that shows information about a video's audio. I use the AudioContext interface to get audio samples from a HTML5 video element. It works fine the first time I create the ponent, but when the ponent is unmounted and then recreated at a later point, I get the following error message:
Uncaught InvalidStateError: Failed to execute 'createMediaElementSource' on 'AudioContext': HTMLMediaElement already connected previously to a different MediaElementSourceNode.
Here's how I get the audio:
const video = document.querySelectorAll('video')[0]
if (!window.audioContext) {
window.audioContext = new (window.AudioContext || window.webkitAudioContext)
}
if (!this.source && !this.scriptNode) {
this.source = window.audioContext.createMediaElementSource(video)
this.scriptNode = window.audioContext.createScriptProcessor(4096, 1, 1)
}
this.scriptNode.onaudioprocess = (evt) => {
// Processing audio works fine...
}
this.source.connect(this.scriptNode)
this.scriptNode.connect(window.audioContext.destination)
And when the ponent is unmounted I do:
if (this.source && this.scriptNode) {
this.source.disconnect(this.scriptNode)
this.scriptNode.disconnect(window.audioContext.destination)
}
I thought this would put me in a state where I can safely create and connect new nodes. But the next time the ponent is mounted, this block throws the error mentioned earlier:
if (!this.source && !this.scriptNode) {
this.source = window.audioContext.createMediaElementSource(video) // this throws the error
this.scriptNode = window.audioContext.createScriptProcessor(4096, 1, 1)
}
I could get it to work by making everything global, i.e. putting source
and scriptNode
on window
rather than this
. But that won't work if my video element changes. What's the correct way to do this?
- Did you get around this one, by chance? I'm facing the exact same problem and still haven't found a solution. – spoutnik Commented Jul 10, 2019 at 17:26
1 Answer
Reset to default 3You're not destroying the node you created with context.createMediaSourceElement. You have your on page video element which hasn't changed, all you've done is disconnected the video audio stream from your audio graph. Therefore the video element is still bound to an AudioNode, in this case 'this.source'. Instead of trying to recreate source just detect if the source is already defined.
if (this.source == undefined) {
// Build element
this.source = window.audioContext.createMediaElementSource(video);
}
this.scriptNode = window.audioContext.createScriptProcessor(4096, 1, 1)
this.source.connect(this.scriptNode);
this.scriptNode.connect(window.audioContext.destination);
本文标签: javascriptProblems disconnecting nodes with AudioContext (Web Audio API)Stack Overflow
版权声明:本文标题:javascript - Problems disconnecting nodes with AudioContext (Web Audio API) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406932a2469012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论