您好我正在制作一个使用Google Chrome打开我的Android相机的程序。当用户按下后退按钮时,我需要知道如何完全关闭镀铬,否则在镀铬时禁用后退按钮。这是我的代码。我想用JavaScript做。
<body style="overflow: hidden">
<video muted autoplay id="myvideo" style="width: 100%; height: auto"></video>
<div class='select'>
<select style="visibility: hidden" id='videoSource'>
</select>
</div>
</body>
<script type="text/javascript">
//Camera
var videoElement = document.querySelector("video");
var videoSelect = document.querySelector("select#videoSource");
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
//Checks for the sources available of cameras
function gotSources(sourceInfos) {
for (var i = 0; i != sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
var option = document.createElement("option");
option.value = sourceInfo.id;
if (sourceInfo.kind === 'video') {
option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
videoSelect.appendChild(option);
if (i == sourceInfos.length - 1) {
option.selected = true;
start();
}
} else {
console.log('Some other kind of source: ', sourceInfo);
}
}
}
if (typeof MediaStreamTrack === 'undefined') {
alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
} else {
MediaStreamTrack.getSources(gotSources);
}
//Checks if everything is successful
function successCallback(stream) {
window.stream = stream; // make stream available to console
videoElement.src = window.URL.createObjectURL(stream);
videoElement.play();
}
//Checks if an error occurred
function errorCallback(error) {
console.log("navigator.getUserMedia error: ", error);
}
// starts the streaming of the camera
function start() {
if (!!window.stream) {
videoElement.src = null;
window.stream.stop();
}
var videoSource = videoSelect.value;
var constraints = {
video: {
optional: [{ sourceId: videoSource}]
}
};
navigator.getUserMedia(constraints, successCallback, errorCallback);
}
videoSelect.onchange = start;