视频媒体设备将Blob分配给'videoRef.srcObject'代替'src' [英] Video mediaDevices Assign a Blob To 'videoRef.srcObject' In Place of 'src'
问题描述
我正在尝试使blob成为视频元素的src.在以下情况下,我的代码可以正常工作:
I am trying to make a blob the src of a video element. My code is working fine when:
videoRef.src = URL.createObjectURL(blob)
但不建议使用'src'和'URL.createObjectURL'代替使用'srcObject'
but using 'src' and 'URL.createObjectURL' is deprecated in place of using 'srcObject' https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
和 https://developer.mozilla.org/en -US/docs/Web/API/MediaDevices/getUserMedia
我的问题是如何像这样简单地将blob文件添加到srcObject:
My question is how can I simply add a blob file to the srcObject like so:
videoRef.srcObject = blob
当我尝试上述代码时,出现错误:"TypeError:无法在'HTMLMediaElement'上设置'srcObject'属性:提供的值不是'MediaStream'类型的."
When I try the above code I get the error: "TypeError: Failed to set the 'srcObject' property on 'HTMLMediaElement': The provided value is not of type 'MediaStream'."
如何不使用不赞成使用的videoRef.src并将blob直接应用于videoRef.srcObject?还是blob类型使用src可以,并且只有流不能使用src吗?
How can I not use the deprecated videoRef.src and apply a blob directly to videoRef.srcObject ? Or is it ok for blob type to use src, and only streams can't use src ?
推荐答案
URL.createObjectURL
仅不推荐用于 流 ,而不是blob和媒体源.
URL.createObjectURL
is only deprecated for streams, not blobs and mediasources.
您引用的MDN警告位于标题为将对象URL用于媒体流.警告本身说:
The MDN warning you reference is under the section titled Using object URLs for media streams. The warning itself says:
如果您仍有依赖createObjectURL()的代码将 流 附加到媒体元素
If you still have code that relies on createObjectURL() to attach streams to media elements
由于流本质上是本地对象,因此正在努力废弃URL.createObjectURL
专门针对 流 .
There's an effort to deprecate URL.createObjectURL
specifically around streams, because streams are inherently local objects.
TypeError:无法在"HTMLMediaElement"上设置"srcObject"属性
TypeError: Failed to set the 'srcObject' property on 'HTMLMediaElement
看来您的浏览器尚未为Blob实现srcObject
.这是常见的atm.
Looks like your browser hasn't implemented srcObject
for blobs yet. This is common atm.
例如Chrome和Firefox都对流的srcObject具有部分支持唯一,但不包括Blob,文件或媒体源.
E.g. both Chrome and Firefox have partial support for srcObject for streams only, but not blob, file, or mediasource.
MDN与此相呼应:>
MDN on srcObject echoes this:
截至2017年11月,浏览器仅支持MediaStream.对于MediaSource,Blob和File,您必须使用URL.createObjectURL()创建一个URL,并将其分配给HTMLMediaElement.src.
As of November 2017, browsers only support MediaStream. For MediaSource, Blob and File, you have to create a URL with URL.createObjectURL() and assign it to HTMLMediaElement.src.
这篇关于视频媒体设备将Blob分配给'videoRef.srcObject'代替'src'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!