track.stop 不再关闭相机
track.stop is not turning the camera off anymore
我有一个网页,我想让用户用他的 laptop/phone 相机拍照。
一旦他点击一个按钮,就会显示一个模态,下面的 js 将启动相机流来拍照:
function startStreaming() {
if (null != cameraStream) {
var track = cameraStream.getTracks()[0];
track.stop();
stream.load();
cameraStream = null;
}
//const audioSource = audioInputSelect.value;
const videoSource = videoSelect.value;
const constraints = {
//audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
video: {
deviceId: videoSource ? {
exact: videoSource
} : undefined
}
};
navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
var mediaSupport = 'mediaDevices' in navigator;
if (mediaSupport && null == cameraStream) {
const videoSource = videoSelect.value;
const constraints = {
video: {
deviceId: videoSource ? {
exact: videoSource
} : undefined
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then(function (mediaStream) {
cameraStream = mediaStream;
stream.srcObject = mediaStream;
stream.play();
})
.catch(handleError);
} else {
alert('Your browser does not support media devices.');
return;
}
}
这是由
触发的
$('#photoStudio').on('show.bs.modal', function (event) {
navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
startStreaming();
});
然后当我关闭模式时我想停止流式传输但我相机旁边的 LED 指示灯仍然亮着)
$('#photoStudio').on('hide.bs.modal', function (event) {
stopStreaming();
});
其中 stopStreaming()
是:
function stopStreaming() {
if (null != cameraStream) {
var track = cameraStream.getTracks()[0];
track.stop();
stream.load();
cameraStream = null;
}
}
我没有收到任何类型的错误,而且我找不到调试相机仍然 运行 的方法。我是否遗漏了 stopStreaming 函数中的任何内容?
如果任何曲目没有停止,那么您的相机仍将处于活动状态。在您的 stopStreaming
函数中,您只停止返回数组中的第一首曲目。
如果您改为遍历曲目,您可能会发现您当前不在的曲目:
function stopStreaming() {
if (null != cameraStream) {
var tracks = cameraStream.getTracks();
// stop all the tracks, not just the first
tracks.forEach((track) => {
track.stop();
});
stream.load();
cameraStream = null;
}
}
this.camera_stream.getTracks().forEach((track) => {
console.log(track);
track.stop();
**track.enabled = false**
});
video.load()
this.camera_stream = null
我有一个网页,我想让用户用他的 laptop/phone 相机拍照。 一旦他点击一个按钮,就会显示一个模态,下面的 js 将启动相机流来拍照:
function startStreaming() {
if (null != cameraStream) {
var track = cameraStream.getTracks()[0];
track.stop();
stream.load();
cameraStream = null;
}
//const audioSource = audioInputSelect.value;
const videoSource = videoSelect.value;
const constraints = {
//audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
video: {
deviceId: videoSource ? {
exact: videoSource
} : undefined
}
};
navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
var mediaSupport = 'mediaDevices' in navigator;
if (mediaSupport && null == cameraStream) {
const videoSource = videoSelect.value;
const constraints = {
video: {
deviceId: videoSource ? {
exact: videoSource
} : undefined
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then(function (mediaStream) {
cameraStream = mediaStream;
stream.srcObject = mediaStream;
stream.play();
})
.catch(handleError);
} else {
alert('Your browser does not support media devices.');
return;
}
}
这是由
触发的 $('#photoStudio').on('show.bs.modal', function (event) {
navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
startStreaming();
});
然后当我关闭模式时我想停止流式传输但我相机旁边的 LED 指示灯仍然亮着)
$('#photoStudio').on('hide.bs.modal', function (event) {
stopStreaming();
});
其中 stopStreaming()
是:
function stopStreaming() {
if (null != cameraStream) {
var track = cameraStream.getTracks()[0];
track.stop();
stream.load();
cameraStream = null;
}
}
我没有收到任何类型的错误,而且我找不到调试相机仍然 运行 的方法。我是否遗漏了 stopStreaming 函数中的任何内容?
如果任何曲目没有停止,那么您的相机仍将处于活动状态。在您的 stopStreaming
函数中,您只停止返回数组中的第一首曲目。
如果您改为遍历曲目,您可能会发现您当前不在的曲目:
function stopStreaming() {
if (null != cameraStream) {
var tracks = cameraStream.getTracks();
// stop all the tracks, not just the first
tracks.forEach((track) => {
track.stop();
});
stream.load();
cameraStream = null;
}
}
this.camera_stream.getTracks().forEach((track) => {
console.log(track);
track.stop();
**track.enabled = false**
});
video.load()
this.camera_stream = null