1

jsfiddle ( https://jsfiddle.net/kalyansai99/mm1b74uy/22/ ) 包含用户可以在手机的前后摄像头之间切换的代码。

在少数手机(Moto g5 plus、Moto E3 等 - Chrome 浏览器)和少数手机(Mi Redimi Note 4 - Chrome 浏览器)中,当我切换到后置摄像头时,最初的流正在加载轨道“readyState”为“live”。但是当我要在视频播放器中播放流时,“readyState”正在更改为“结束”,并且视频标签上显示黑屏。

不知道发生了什么。有什么线索吗?

JSF中间代码

var player = document.getElementById('player');
var flipBtn = document.getElementById('flipBtn');
var deviceIdMap = {};
var front;

var constraints = {
    audio: false,
    video: {
        frameRate: 1000
    }
};

var gotDevices = function (deviceList) {
    var length = deviceList.length;
    console.log(deviceList);
    for (var i = 0; i < length; i++) {
        var deviceInfo = deviceList[i];
        if (deviceInfo.kind === 'videoinput') {
            if (deviceInfo.label.indexOf('front') !== -1) {
                deviceIdMap.front = deviceInfo.deviceId;
            } else if (deviceInfo.label.indexOf('back') !== -1) {
                deviceIdMap.back = deviceInfo.deviceId;
            }
        }
    }
    if (deviceIdMap.front) {
        constraints.video.deviceId = {exact: deviceIdMap.front};
        front = true;
    } else if (deviceIdMap.back) {
        constraints.video.deviceId = {exact: deviceIdMap.back};
        front = false;
    }
    console.log('deviceIdMap - ', deviceIdMap);
};

var handleError = function (error) {
    console.log('navigator.getUserMedia error: ', error);
};

function handleSuccess(stream) {
    window.stream = stream;
    // this is a video track as there is no audio track
    console.log("Track - ", window.stream.getTracks()[0]);
    console.log('Ready State - ', window.stream.getTracks()[0].readyState);
    if (window.URL) {
        player.src = window.URL.createObjectURL(stream);
    } else {
        player.src = stream;
    }
    player.onloadedmetadata = function (e) {
    		console.log('Ready State - 3', window.stream.getTracks()[0].readyState);
        player.play();
        console.log('Ready State - 4', window.stream.getTracks()[0].readyState);
    }
    console.log('Ready State - 2', window.stream.getTracks()[0].readyState);
}

navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);

flipBtn.addEventListener('click', function () {
		if (window.stream) {
      window.stream.getTracks().forEach(function(track) {
        track.stop();
      });
    }
  	if (front) {
      constraints.video.deviceId = {exact: deviceIdMap.back};
    } else {
      constraints.video.deviceId = {exact: deviceIdMap.front};
    }
  	front = !front;
  	navigator.getUserMedia(constraints, handleSuccess, handleError);
}, false);

console.log(constraints);
navigator.getUserMedia(constraints, handleSuccess, handleError);
#player {
  width: 320px;
}

#flipBtn {
  width: 150px;
  height: 50px;
}
<video id="player" autoplay></video>

<div>
  <button id="flipBtn">
      Flip Camera
  </button>
</div>

4

1 回答 1

6

替换track.stop()track.enabled=false并在将轨道添加到流时,使用启用它track.enabled=true

当我们停止跟踪并且永远不能再次使用时,该MediaStream.readyState属性将更改为“结束”。因此使用停止是不明智的。更多参考:

https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/readyState

https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/stop

于 2018-03-14T10:42:35.847 回答