![](/img/trans.png)
[英]Is it possible to use the MediaRecorder API with html5 video?
[英]iOS MediaRecorder - Record Video Via Camera Through MediaRecorder and HTML5 Video
问题:
ng build
调查:
websockets
、 MediaRecorder
和getUserDevices()
有关的各种网站MediaRecorder
是一项实验性功能,需要手动启用。其他:
可悲的是我不知道该怎么办。 因为文档说 iOS 不支持录音 API。
任何提示、链接、资源或代码将不胜感激。
我想你已经找到了你问题的答案。
使用您当前的实现,您将无法支持 Safari 或任何 iOS 浏览器(它们都是引擎盖下的 Webkit/Safari)。
https://caniuse.com/?search=MediaRecorder
您可以启用一些标志来使其工作,但这不是您通常可以要求客户做的事情:)
道格
从 Safari 14.5 开始,您可以使用 MediaRecorder API。
这是一个使用 MediaRecorder 和getUserMedia()
的工作示例。
startIOSVideoRecording: function() {
console.log("setting up recorder");
let self = this;
this.data = [];
if (MediaRecorder.isTypeSupported('video/mp4')) {
// IOS does not support webm! So you have to use mp4.
var options = {mimeType: 'video/mp4', videoBitsPerSecond : 1000000};
} else {
// video/webm is recommended for non IOS devices
console.error("ERROR: Is this really an IOS device??");
var options = {mimeType: 'video/webm'};
}
// this has to be a canvas element (not video!)
let stream = document.getElementById('self-canvas').captureStream(15);
this.recorder = new MediaRecorder(stream, options);
this.recorder.ondataavailable = function(evt) {
if (evt.data && evt.data.size > 0) {
self.data.push(evt.data);
console.log('chunk size: ' + evt.data.size);
}
}
this.recorder.onstop = function(evt) {
console.log('recorder stopping');
var blob = new Blob(self.data, {type: "video/mp4"});
// do something with the blob
}
this.recorder.start(1000);
looper();
},
请记住,IOS 仍然根本不支持video.captureStream()
或video/webm
,因此您需要设置一个视频元素来连接navigator.mediaDevices.getUserMedia()
和一个用于 MediaRecorder 的 canvas 元素。
另外(在 IOS 上)你一次只能声明一个getUserMedia()
stream ! 因此最好使用this.video
或this.localStream
使视频元素或 stream 全局化,以防其他元素想要访问 stream。
this.localStream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {width: 430, height: 430, facingMode: "user"}
});
let self = this;
self.video = document.createElement('video');
self.video.srcObject = this.localStream;
const canvas = document.getElementById("self-canvas");
const ctx = canvas.getContext('2d');
function draw_2DCanvas(){
ctx.clearRect(0,0, canvas.width, canvas.height);
ctx.drawImage(self.video, 0, 0, 430, 430);
}
requestAnimationFrame(draw_2DCanvas);
}
draw_2DCanvas();
在任何其他设备上,您可以只使用video.captureStream()
而无需 go 尽管设置 canvas 元素的痛苦。
通常也推荐使用video/webm
。
重要的提示
如果您不打算以任何方式编辑视频,直接将localStream
传递给 MediaRecorder 也可以,并且还可以让您录制用户的音频。
this.recorder = new MediaRecorder(this.localStream, options);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.