HTML

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>recorded</title>
</head>
<body> <video id="gum" autoplay muted></video>
<video id="recorded" autoplay loop></video> <div>
<button id="opencamera">Open camera</button>
<button id="record" disabled>Start Recording</button>
<button id="play" disabled>Play</button>
<button id="download" disabled>Download</button>
</div>
</body>
</html>

JavaScript

'use strict';

var mediaRecorder;
var recordedBlobs; var gumVideo = document.querySelector('video#gum');
var recordedVideo = document.querySelector('video#recorded'); var recordButton = document.querySelector('button#record');
var playButton = document.querySelector('button#play');
var downloadButton = document.querySelector('button#download');
recordButton.onclick = toggleRecording;
playButton.onclick = play;
downloadButton.onclick = download; var isSecureOrigin = location.protocol === 'https:' ||
location.hostname === 'localhost';
if (!isSecureOrigin) {
alert('getUserMedia() must be run from a secure origin: HTTPS or localhost.' +
'\n\nChanging protocol to HTTPS');
location.protocol = 'HTTPS';
} var constraints = {
audio: true,
video: true
}; function handleSuccess(stream) {
recordButton.disabled = false;
console.log('getUserMedia() got stream: ', stream);
window.stream = stream;
if (window.URL) {
gumVideo.src = window.URL.createObjectURL(stream);
} else {
gumVideo.src = stream;
}
} function handleError(error) {
console.log('navigator.getUserMedia error: ', error);
} document.querySelector('#opencamera').onclick = function(){
this.disabled = true;
navigator.mediaDevices.getUserMedia(constraints).
then(handleSuccess).catch(handleError);
}; recordedVideo.addEventListener('error', function(ev) {
console.error('MediaRecording.recordedMedia.error()');
alert('Your browser can not play\n\n' + recordedVideo.src
+ '\n\n media clip. event: ' + JSON.stringify(ev));
}, true); function handleDataAvailable(event) {
if (event.data && event.data.size > 0) {
recordedBlobs.push(event.data);
}
} function handleStop(event) {
console.log('Recorder stopped: ', event);
} function toggleRecording() {
if (recordButton.textContent === 'Start Recording') {
startRecording();
} else {
stopRecording();
recordButton.textContent = 'Start Recording';
playButton.disabled = false;
downloadButton.disabled = false;
}
} function startRecording() {
recordedBlobs = [];
var options = {mimeType: 'video/webm;codecs=vp9'};
if (!MediaRecorder.isTypeSupported(options.mimeType)) {
console.log(options.mimeType + ' is not Supported');
options = {mimeType: 'video/webm;codecs=vp8'};
if (!MediaRecorder.isTypeSupported(options.mimeType)) {
console.log(options.mimeType + ' is not Supported');
options = {mimeType: 'video/webm'};
if (!MediaRecorder.isTypeSupported(options.mimeType)) {
console.log(options.mimeType + ' is not Supported');
options = {mimeType: ''};
}
}
}
try {
mediaRecorder = new MediaRecorder(window.stream, options);
} catch (e) {
console.error('Exception while creating MediaRecorder: ' + e);
alert('Exception while creating MediaRecorder: '
+ e + '. mimeType: ' + options.mimeType);
return;
}
console.log('Created MediaRecorder', mediaRecorder, 'with options', options);
recordButton.textContent = 'Stop Recording';
playButton.disabled = true;
downloadButton.disabled = true;
mediaRecorder.onstop = handleStop;
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start(10); // collect 10ms of data
console.log('MediaRecorder started', mediaRecorder);
} function stopRecording() {
mediaRecorder.stop();
console.log('Recorded Blobs: ', recordedBlobs);
recordedVideo.controls = true;
} function play() {
var superBuffer = new Blob(recordedBlobs, {type: 'video/webm'});
recordedVideo.src = window.URL.createObjectURL(superBuffer);
} function download() {
var blob = new Blob(recordedBlobs, {type: 'video/webm'});
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'test.webm';
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}

最新文章

  1. blade and soul races guide
  2. springmvc Failed to load resource: the server responded with a status of 404 (Not Found)
  3. Java并发编程实战---第六章:任务执行
  4. 《C语言编写 学生成绩管理系统》
  5. SQL Server数据库连接类SQLHelper.cs
  6. BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
  7. poj 2425 A Chess Game(SG函数)
  8. ZOJ 3903 Ant(公式推导)
  9. [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types
  10. checkbox之checked的方法(attr和prop)区别
  11. HDU 1232 畅通工程(最小生成树+并查集)
  12. 网络组Network Teaming
  13. angular-file-upload封装为指令+图片尺寸限制
  14. mac下安装ELK
  15. Python_异常处理结构与调试
  16. Windows To Go入坑记录
  17. Java 多线程系列 CountDownLatch
  18. 关于TypeError: strptime() argument 1 must be str, not bytes解析
  19. jmeter(二十三)分布式测试
  20. 查询的model里面 一般都要有一个要返回的model做属性 ;查询前要传入得参数,查询后返回的参数 都要集合在一个model中

热门文章

  1. 静态方法中不能使用 $this
  2. SVM 与 LR的异同
  3. AtCoder Grand Contest 022
  4. 顺序表ans链性表
  5. 九度oj 题目1397:查找数段
  6. linux shell脚本监控进程是否存在
  7. FOJ Problem 2253 Salty Fish
  8. node起始——安装并且新建一个node项目
  9. jquery插件的基本写法
  10. poj 1066 Treasure Hunt 线段相交