Chrome最新版本(版本47.0.2526.80 m):navigator.webkitGetUserMedia返回一个没有停止功能的流

时间:2015-12-09 11:36:37

标签: javascript jquery angularjs google-chrome

我已经实现了一段代码来访问Chrome浏览器上的摄像头并在使用后将其释放。 的代码:

启动相机:

navigator.webkitGetUserMedia($scope.options.videoObj, function (stream) {
    $scope.options.video.src = window.URL.createObjectURL(stream);
    $scope.options.video.play();
    $scope.options.localMediaStream = stream;                   
}, $scope.options.errBack);

停止相机:

    $scope.options.video.pause();               
    $scope.options.localMediaStream.stop();

但是,在最新升级 Chrome浏览器之后,即版本47.0.2526.80 m, $ scope.options.localMediaStream没有" stop"功能

所以,一切都在破碎。 我需要释放相机访问权限,以便其他浏览器可以访问相机。 (以前停止功能正在做这项工作)

如果有人有任何解决方案,请帮助我。

3 个答案:

答案 0 :(得分:1)

似乎chrome已将其删除,因为此功能已弃用。

  

以后版本的Chrome支持无前缀MediaDevices.getUserMedia(),取代了这种已弃用的方法。

enter image description here

以及.stop()方法的兼容性表:

enter image description here

相反,您会使用MediaDevices.getUserMedia()

navigator.mediaDevices.getUserMedia(constraints).then(function(mediaStream) { ... })

答案 1 :(得分:1)

正如@Jai在回答中所说,Chrome最终实施了新的navigator.mediaDevices.getUserMedia()方法。

According to the specs,要停止播放,您现在必须调用每个stop()对象的mediaStreamTrack方法,而不是MediaStream个对象。

完整代码:

var video = document.querySelector('video');
var start = function(){
    navigator.mediaDevices.getUserMedia({video:true}).then(function(mediaStream){
        window.stream = mediaStream;
        video.src = URL.createObjectURL(mediaStream);
        video.play();
    });
}
var stop = function(){
    if(stream.stop) {
        stream.stop(); // FF and other not yet updated browsers
    } else {
        // updated browsers
        var tracks = stream.getTracks();
        for(var i=0; i<tracks.length; i++) {
            tracks[i].stop();
        }
    }
    URL.revokeObjectURL(video.src);
}
或者,要仅释放相机,您可以拨打
stream.getVideoTracks()[0].stop()

答案 2 :(得分:0)

$ scope.options.localMediaStream = NULL;

相关问题