Skip to content

Instantly share code, notes, and snippets.

@er-rishi

er-rishi/stream.js

Created Jun 5, 2014
Embed
What would you like to do?
/*
* Get mediastream
*/
var userStream;
navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
navigator.getUserMedia (
// constraints
{
video: true,
audio: true
},
// successCallback
function(localMediaStream) {
// Assign mediastream to global variable
userStream = localMediaStream;
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
// Do something with the video here, e.g. video.play()
},
// errorCallback
function(err) {
console.log("The following error occured: " + err);
}
);
} else {
console.log("getUserMedia not supported");
}
/*
* To remove Audio Track
*/
var audioTrack = userStream.getAudioTracks();
if (audioTrack.length > 0) {
userStream.removeTrack(audioTrack[0]);
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(userStream);
}
/*
* To remove Video Track
*/
var videoTrack = userStream.getVideoTracks();
if (videoTrack.length > 0) {
userStream.removeTrack(videoTrack[0]);
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(userStream);
// Option-1
$('video').css('display', 'none');
// Option-2
var audio = document.querySelector('audio');
audio.src = window.URL.createObjectURL(userStream);
$('video').attr('src', '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment