【问题标题】:Stop video with getUserMedia使用 getUserMedia 停止视频
【发布时间】:2016-03-09 05:28:35
【问题描述】:

我正在尝试了解有关流式网络摄像头的更多信息,但我一直无法停止视频我希望有人可以帮助我停止视频

var videoDiv = $("#video"),
	vendorUrl = window.URL || window.webkitURL;
	navigator.getMedia = 	navigator.getUserMedia ||
							navigator.webkitGetUserMedia ||
							navigator.mozGetUserMedia ||
							navigator.oGetUserMedia ||
							navigator.msGetUserMedia;

function captureWebcam(video, audio){
	navigator.getMedia({
		video: video,
		audio: audio
	}, function(stream){
		localStream = stream;
		videoDiv.attr("src", vendorUrl.createObjectURL(localStream))
	}, function(error){
		// An error occured
		// error.code
		console.log(error)
	});	
}		
$("#stop").on("click", function(){

	videoDiv.attr("src", "")	
	//captureWebcam(false, false)
    // Stop the video
});	
$("#start").on("click", function(){
	captureWebcam(true, false)
});
<video id="video" width="400" height="300"></video>
<button id="start">START!</button>
<button id="stop">STOP!</button>

【问题讨论】:

标签: javascript webrtc getusermedia


【解决方案1】:

您需要使用getTrack() 才能停止流。

var videoDiv = document.getElementById("video"),
    vendorUrl = window.URL || window.webkitURL;
    navigator.getMedia =    navigator.getUserMedia ||
                            navigator.webkitGetUserMedia ||
                            navigator.mozGetUserMedia ||
                            navigator.oGetUserMedia ||
                            navigator.msGetUserMedia;

var MediaStream;
function captureWebcam(video, audio){
    navigator.getMedia({
        video: video,
        audio: audio
    }, function(stream){
        videoDiv.src = vendorUrl.createObjectURL(stream);
        MediaStream = stream.getTracks()[0]; // create the stream tracker
    }, function(error){
        // An error occured
        // error.code
        console.log(error)
    }); 
}       
$("#stop").on("click", function(){
   // Stop the tracked stream
   MediaStream.stop()
}); 
$("#start").on("click", function(){
    captureWebcam(true, false)
});

fiddle给你看看

【讨论】:

  • 小提琴不工作....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多