forked from mozdevs/MediaRecorder-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediastream-constructor.js
43 lines (34 loc) · 1.42 KB
/
mediastream-constructor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// This example uses MediaDevices.getUserMedia to get an audio and video stream,
// then we create a new stream using the MediaStream constructor, extract the tracks
// from the original stream, add them to the new stream we created and finally display
// it in a video element
//
// The relevant functions in use are:
//
// navigator.mediaDevices.getUserMedia -> to get live audio + video stream from webcam
// MediaStream.getTracks(), MediaStream.addTrack() -> to get and add tracks from/to a stream
// MediaStream() constructor -> to create a new stream
// URL.createObjectURL -> to create a URL for the stream, which we can use as src for the video
window.onload = function () {
// request video and audio stream from the user's webcam
navigator.mediaDevices.getUserMedia({
audio: true,
video: true
})
.then(function (stream) {
var video = document.createElement('video');
var main = document.querySelector('main');
main.appendChild(video);
// Create a new stream
var newStream = new MediaStream();
// If we display this stream right now, it will have nothing to show
// We need to add some tracks, which we'll take from the initial stream
var tracks = stream.getTracks();
tracks.forEach(function(t) {
newStream.addTrack(t);
});
// Finally we create a URL to display this newStream we created
video.src = URL.createObjectURL(newStream);
video.play();
});
};