我使用以下JavaScript代码打开了一个网络摄像头:
const stream = await navigator.mediaDevices.getUserMedia({ /* ... */ });
是否有任何JavaScript代码停止或关闭网络摄像头?
我使用以下JavaScript代码打开了一个网络摄像头:
const stream = await navigator.mediaDevices.getUserMedia({ /* ... */ });
是否有任何JavaScript代码停止或关闭网络摄像头?
当前回答
使用下列函数:
// stop both mic and camera
function stopBothVideoAndAudio(stream) {
stream.getTracks().forEach(function(track) {
if (track.readyState == 'live') {
track.stop();
}
});
}
// stop only camera
function stopVideoOnly(stream) {
stream.getTracks().forEach(function(track) {
if (track.readyState == 'live' && track.kind === 'video') {
track.stop();
}
});
}
// stop only mic
function stopAudioOnly(stream) {
stream.getTracks().forEach(function(track) {
if (track.readyState == 'live' && track.kind === 'audio') {
track.stop();
}
});
}
其他回答
使用下列函数:
// stop both mic and camera
function stopBothVideoAndAudio(stream) {
stream.getTracks().forEach(function(track) {
if (track.readyState == 'live') {
track.stop();
}
});
}
// stop only camera
function stopVideoOnly(stream) {
stream.getTracks().forEach(function(track) {
if (track.readyState == 'live' && track.kind === 'video') {
track.stop();
}
});
}
// stop only mic
function stopAudioOnly(stream) {
stream.getTracks().forEach(function(track) {
if (track.readyState == 'live' && track.kind === 'audio') {
track.stop();
}
});
}
你需要停止所有的轨迹(来自摄像头,麦克风):
localStream.getTracks().forEach(track => track.stop());
有溪流形式成功的参考吗
var streamRef;
var handleVideo = function (stream) {
streamRef = stream;
}
//this will stop video and audio both track
streamRef.getTracks().map(function (val) {
val.stop();
});
不要使用stream.stop(),它已被弃用
MediaStream用法
使用stream.getTracks()。forEach(track => track.stop())
请检查这个:https://jsfiddle.net/wazb1jks/3/
导航器。getUserMedia(mediaConstraints,函数(流){ 窗口。streamReference =流; }, onMediaError);
停止记录
函数stopStream() { if (!window.streamReference)返回; window.streamReference.getAudioTracks () .forEach(函数(跟踪){ track.stop (); }); window.streamReference.getVideoTracks () .forEach(函数(跟踪){ track.stop (); }); 窗口。streamReference = null; }