Click here to Skip to main content
15,883,535 members
Articles / Programming Languages / Javascript

Use JavaScript and WebRTC API to access your Camera and Microphone from browser

Rate me:
Please Sign up or sign in to vote.
4.79/5 (14 votes)
13 Feb 2013CPOL4 min read 76.9K   2.4K   33  
How to access Camera and Microphone using JavaScript, HTML5 and a new browser
<!DOCTYPE html><html>
<head> 
<meta charset="UTF-8" />
<meta name="author" content="Saad Mousliki" />
<title>Use WebCam In Browser by Saad Mousliki</title> 
<script type="text/javascript"> 
window.onload = function () {

           var isWebkit = false;    

          
 
function onSuccess(stream) { 
var videoElement = document.getElementById("video1"); 
var StreamSource; 
	videoElement.autoplay = true; 
if (!isWebkit) {
        StreamSource = stream;
    }
    else {
        StreamSource = webkitURL.createObjectURL(stream);
    } 
videoElement.src = StreamSource; 
}
 
 function onError(er) {

    alert("Error function reached, Can't Access the user devices");

} 
 try {
 	//for Opera or others 
	
	isWebkit = false; 
	   navigator.webkitGetUserMedia({ video: true, audio: true }, onSuccess, onError); 
} 
    catch (e) {
    	//for webkit
     	isWebkit = true;	
  navigator.webkitGetUserMedia('video, audio', onSuccess, onError); 
}  
 
}

</script> 
</head> 
<body> 
<div style="float:left"> 
<p>Display your WebCam</p> 
<video width="320" height="240" id="video1"></video> 
</div> 
</body> 
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Writer SaadMos
Morocco Morocco
currently an IT Manager (ITSM), with background in Web, Mobile and Could
specialized in Web and software: JavaScript; Wakanda; ASP.NET, C #, WPF, JAVA, C... (SQL Server, jQuery, AJAX, ...)
I am also interested to mobile: Kinect, iOS, hybrid mobile app, and Android
To contact me : saadmos06@gmail.com

Comments and Discussions