attachMediaStream
What is this?
A tiny browser module for attaching a media stream to a video element. It handles the differences between browsers.
Suitable for use with browserify/CommonJS on the client.
If you're not using browserify or you want AMD support use attachmediastream.bundle.js
.
Installing
npm install attachmediastream
How to use it
Makes it easy to attach video streams to video tags.
var getUserMedia = require('getusermedia');
var attachMediaStream = require('attachmediastream');
getUserMedia(function (err, stream) {
if (err) {
console.log('failed');
} else {
console.log('got a stream', stream);
var videoEl = attachMediaStream(stream, document.getElementById('myVideo'));
var generatedVideoEl = attachMediaStream(stream);
var videoEl = attachMediaStream(stream, someEl, {
autoplay: true,
mirror: true,
muted: true
});
}
});
Why?
Browsers used to to this very differently. This is now less true than it used to be. It's fairly safe to just use URL.createObjectUrl(stream)
.
However, it's nice to know it will work if that's not true and it's also handy to be able to control mirroring, muting, autoplay in one shot with sane defaults.
Caveats
As of writing this, FireFox doesn't let you show local video feed more than once on a page and trying to do so will result in none of them playing and it appearing broken.
As a result the test.html
file won't work in FireFox stable unless you do one at a time.
Other Details
The module's main function returns the element if successful and false
otherwise. But if you're able to getUserMedia to begin with, attaching it shouldn't really fail.
License
MIT
Created By
If you like this, follow @HenrikJoreteg on twitter.