Skip to content

Instantly share code, notes, and snippets.

@er-rishi
Created June 5, 2014 08:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save er-rishi/5b1faaa03d16b9731c1c to your computer and use it in GitHub Desktop.
Save er-rishi/5b1faaa03d16b9731c1c to your computer and use it in GitHub Desktop.
/*
* 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