This repository has been archived by the owner on Jan 18, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example that demonstrates taking a track out of a stream and addi…
…ng it to another stream
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!doctype html> | ||
<head> | ||
<title>MediaRecorder examples - Take track out of stream</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
||
<link rel="stylesheet" href="styles.css" type="text/css"> | ||
<script src="take-tracks-out-of-streams.js"></script> | ||
</head> | ||
<body> | ||
<header> | ||
<h1><a href="index.html">MediaRecorder examples</a></h1> | ||
<a href="https://github.com/mozdevs/mediarecorder"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a> | ||
<p>Take <code>MediaStreamTrack</code> out of a <code>MediaStream</code> and add it to another <code>MediaStream</code>.</p> | ||
</header> | ||
<main> | ||
</main> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// This example uses MediaDevices.getUserMedia to get an audio and video stream, | ||
// and then display it in a video element | ||
// | ||
// The relevant functions in use are: | ||
// | ||
// navigator.mediaDevices.getUserMedia -> to get live video stream from webcam | ||
// AudioContext.createMediaStreamDestination -> to create a stream with audio out of a Web AudioContext | ||
// Stream.getAudioTracks() -> to get only the audio tracks from a stream | ||
// Stream.addTrack() -> to add a track to an existing stream | ||
// URL.createObjectURL -> to create a URL for the stream, which we can use as src for the video | ||
|
||
|
||
window.onload = function () { | ||
|
||
// request video stream from the user's webcam | ||
navigator.mediaDevices.getUserMedia({ | ||
video: true | ||
}) | ||
.then(function (stream) { | ||
var video = document.createElement('video'); | ||
video.controls = true; | ||
var main = document.querySelector('main'); | ||
main.appendChild(video); | ||
|
||
var audioContext = new AudioContext(); | ||
var oscillator = audioContext.createOscillator(); | ||
oscillator.frequency.value = 110; | ||
var streamAudioDestination = audioContext.createMediaStreamDestination(); | ||
oscillator.connect(streamAudioDestination); | ||
oscillator.start(); | ||
|
||
// add audio track from audiocontext with beep | ||
var audioStream = streamAudioDestination.stream; | ||
var audioTracks = audioStream.getAudioTracks(); | ||
var firstAudioTrack = audioTracks[0]; | ||
stream.addTrack(firstAudioTrack); | ||
|
||
video.src = URL.createObjectURL(stream); | ||
video.play(); | ||
|
||
}); | ||
|
||
}; | ||
|