Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.
Demande à l'utlisateur la permission d'utiliser un dispositif media tels qu'un microphone ou un camera. Si l'utilisateur donne cette permission, la methode successCallback
est invoquée avec un objet LocalMediaStream
comme argument.
Syntax
navigator.getUserMedia(constraints, successCallback, errorCallback);
Exemple
Voici un exemple de l'utilisation de getUserMedia()
, avec les différentes mises en oeuvres pour couvrir les préfixes navigateurs.
navigator.getUserMedia = ( navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); if (navigator.getUserMedia) { navigator.getUserMedia ( // constraints { video: true, audio: true }, // successCallback function(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"); }
Parameters
Parameter | Required/Optional | Description |
---|---|---|
constraints |
Required | The media types that support the LocalMediaStream object returned in the successCallback . |
successCallback |
Required | The function on the calling application to invoke when passing the LocalMediaStream object. |
errorCallback |
Optional (Required in Firefox) | The function on the calling application to invoke if the call fails. Please note, this argument is required in recent versions of Firefox. Not providing this argument will throw an NS_ERROR_XPC_NOT_ENOUGH_ARGS error. |
Contraintes
Le paramètre de contrainte est un objet MediaStreamConstraints
qui contient 2 boolean: video
et audio
. Celui-ci decrit le type de media supporté par l'objet LocalMediaStream
. Au moins un de ces media doit être specifié pour valider le paramètre de contrainte. Si une des contraintes spécifiée n'est pas supportée pas le navigateur, getUserMedia()
invoque la méthode errorCallback avec comme erreur NOT_SUPPORTED_ERROR
. Si le navigateur ne peut trouver une piste media du type spécifié, getUserMedia()
invoque la méthode errorCallback
avec l'erreur MANDATORY_UNSATISFIED_ERR
.
If the value or the member is not specified in the object, the value for the member defaults to false. The following demonstrates how to set the constraints for both audio and video:
{ video: true, audio: true }
successCallback
The getUserMedia()
function will call the function specified in the successCallback
with the LocalMediaStream
object that contains the media stream. You may assign that object to the appropriate element and work with it, as shown in the following example:
function(localMediaStream) { var video = document.querySelector('video'); video.src = window.URL.createObjectURL(localMediaStream); video.onloadedmetadata = function(e) { // Do something with the video here. }; },
errorCallback
The getUserMedia()
function will call the function specified in the errorCallback
with a code
argument. The error codes are described as follows:
Error | Description |
---|---|
PERMISSION_DENIED |
The user denied permission to use a media device required for the operation. |
NOT_SUPPORTED_ERROR |
A constraint specified is not supported by the browser. |
MANDATORY_UNSATISFIED_ERROR |
No media tracks of the type specified in the constraints are found. |
Permissions
To use getUserMedia()
in an installable app (for example, a Firefox OS app), you need to specify the following field inside your Manifest file:
"permissions": { "audio-capture": { "description": "Required to capture audio via getUserMedia" } }
See permission: audio-capture for more information.
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Stream API | 21webkit | 17moz | Pas de support | 12 | Pas de support |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Stream API | Pas de support | 24moz | 1.2 for audio moz 1.4 for video moz |
Pas de support | 12 | Pas de support |
Currently, using WebRTC to access the camera is supported in Chrome, Opera, and Firefox 20.
See also
- WebRTC - the introductory page to the API
- MediaStream API - the API for the media stream objects
- Taking webcam photos - a tutorial on using getUserMedia()