[英]How to properly release audioContext when using volume gain
使用WebRTC时,在我停止播放流后,网络摄像头指示灯将熄灭,并且chrome标签上的“红点”将消失。 (如下图所示)
vid = document.createElement('VIDEO');
vid.style.width = 100;
vid.style.height = 100;
vid.style.top = 100;
vid.style.left = 0;
vid.style.position = 'absolute';
vid.style.border = '1px solid red';
vid.id = 'xxx';
document.body.insertBefore(vid,document.body.childNodes[0]);
xxx = document.getElementById('xxx');
var xstream = null;
navigator.webkitGetUserMedia({video:true, audio:true},
function(stream) {
xstream = stream;
xxx.src = window.URL.createObjectURL(stream);
},
function(error) {
console.log('err', error);
}
);
xxx.play();
因此,当我执行以下操作时:
xstream.stop();
我很好,网络摄像头指示灯熄灭,并且选项卡上的红点立即消失。
但是当我使用如下所示的“ audioContext”时:
vid = document.createElement('VIDEO');
vid.style.width = 100;
vid.style.height = 100;
vid.style.top = 100;
vid.style.left = 0;
vid.style.position = 'absolute';
vid.style.border = '1px solid red';
vid.id = 'xxx';
document.body.insertBefore(vid,document.body.childNodes[0]);
xxx = document.getElementById('xxx');
var xstream = null
wp = {};
navigator.webkitGetUserMedia({video:true, audio:true},
function(stream) {
xstream = stream;
wp.audio = {
source: null,
volume: null,
enabled: true
}
wp.audioContext = new window.AudioContext();
wp.audio.source = wp.audioContext.createMediaStreamSource(stream);
wp.audio.volume = wp.audioContext.createGain();
wp.audio.destination = wp.audioContext.createMediaStreamDestination();
wp.audio.outputStream = wp.audio.destination.stream;
wp.audio.source.connect(wp.audio.volume);
wp.audio.volume.connect(wp.audio.destination);
stream.addTrack(wp.audio.outputStream.getAudioTracks()[0]);
stream.removeTrack(stream.getAudioTracks()[0]);
xxx.src = window.URL.createObjectURL(stream);
},
function(error) {
console.log('err', error);
}
);
xxx.play();
如果我只是执行xstream.stop(),尽管网络摄像头指示灯熄灭,但是红点仍然显示。
但是,如果我执行以下操作:
wp.audio.source.mediaStream.stop();
wp.audio.destination.stream.stop();
wp.audio.outputStream.stop();
wp.audio.source.disconnect();
wp.audio.volume.disconnect();
wp.audio.destination = null;
wp.audio.source = null;
wp.audio.volume = null;
wp.audio.outputStream = null;
wp.audioContext.close();
xstream.stop();
xxx.src = '';
有时红点会消失,大多数时候不会消失。 当它消失时,通常需要20-40秒,有时甚至更多。
不知道我在这里做错了什么。
注意:您可以将代码复制并粘贴到控制台中进行尝试
也许您可以尝试在stream.removeTrack之前停止流
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.