使用getUserMedia停止视频

时间:2015-12-04 10:03:41

标签: javascript webrtc getusermedia

我正在尝试了解有关流媒体网络摄像头的更多信息,并且我坚持停止视频我希望有人可以帮助我停止视频



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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

您需要使用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您可以查看