srcObject 特性为 HTMLMediaElement interface sets or returns the object which serves as the source of the media associated with the HTMLMediaElement . The object can be a MediaStream MediaSource , a Blob ,或 File (which inherits from Blob ).

注意: As of March 2020, only Safari supports setting objects other than MediaStream . Until other browsers catch up, for MediaSource , Blob and File , consider falling back to creating a URL with URL.createObjectURL() and assign it to HTMLMediaElement.src . See below for an example.

句法

var sourceObject = HTMLMediaElement.srcObject;
HTMLMediaElement.srcObject = sourceObject;
					

A MediaStream , MediaSource , Blob ,或 File object (though see the compatibility table for what is actually supported).

用法注意事项

Older versions of the Media Source specification required using createObjectURL() to create an object URL then setting src to that URL. Now you can just set srcObject MediaStream 直接。

范例

基本范例

In this example, a MediaStream from a camera is assigned to a newly-created <video> 元素。

const mediaStream = await navigator.mediaDevices.getUserMedia({video: true});
const video = document.createElement('video');
video.srcObject = mediaStream;
					

In this example, a new MediaSource is assigned to a newly-created <video> 元素。

const mediaSource = new MediaSource();
const video = document.createElement('video');
video.srcObject = mediaSource;
					

Supporting fallback to the src property

The examples below support older browser versions that require you to create an object URL and assign it to src if srcObject isn't supported.

首先, MediaStream from a camera is assigned to a newly-created <video> element, with fallback for older browsers.

const mediaStream = await navigator.mediaDevices.getUserMedia({video: true});
const video = document.createElement('video');
if ('srcObject' in video) {
  video.srcObject = mediaStream;
} else {
  // Avoid using this in new browsers, as it is going away.
  video.src = URL.createObjectURL(mediaStream);
}
					

Second, a new MediaSource is assigned to a newly-created <video> element, with fallback for older browsers and browsers that don't yet support assignment of MediaSource 直接。

const mediaSource = new MediaSource();
const video = document.createElement('video');
// Older browsers may not have srcObject
if ('srcObject' in video) {
  try {
    video.srcObject = mediaSource;
  } catch (err) {
    if (err.name != "TypeError") {
      throw err;
    }
    // Even if they do, they may only support MediaStream
    video.src = URL.createObjectURL(mediaSource);
  }
} else {
  video.src = URL.createObjectURL(mediaSource);
}
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'srcObject' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
srcObject Chrome 部分支持 52
部分支持 52
Currently only supports MediaStream 对象。
Edge 部分支持 12
部分支持 12
Currently only supports MediaStream 对象。
Firefox 部分支持 42
部分支持 42
Currently only supports MediaStream 对象。
不支持 18 — 58 Prefixed
Prefixed Implemented with the vendor prefix: moz
IE No Opera 部分支持 39
部分支持 39
Currently only supports MediaStream 对象。
Safari 11 WebView Android 部分支持 52
部分支持 52
Currently only supports MediaStream 对象。
Chrome Android 部分支持 52
部分支持 52
Currently only supports MediaStream 对象。
Firefox Android 部分支持 42
部分支持 42
Currently only supports MediaStream 对象。
不支持 18 — 58 Prefixed
Prefixed Implemented with the vendor prefix: moz
Opera Android 部分支持 41
部分支持 41
Currently only supports MediaStream 对象。
Safari iOS 11 Samsung Internet Android 部分支持 6.0
部分支持 6.0
Currently only supports MediaStream 对象。

图例

完整支持

完整支持

部分支持

部分支持

不支持

不支持

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

元数据

  • 最后修改:
  1. HTMLMediaElement
  2. 特性
    1. audioTracks
    2. autoplay
    3. buffered
    4. controller
    5. controls
    6. controlsList
    7. crossOrigin
    8. currentSrc
    9. currentTime
    10. defaultMuted
    11. defaultPlaybackRate
    12. duration
    13. ended
    14. error
    15. initialTime
    16. loop
    17. mediaGroup
    18. muted
    19. networkState
    20. onerror
    21. paused
    22. playbackRate
    23. readyState
    24. seekable
    25. sinkId
    26. src
    27. srcObject
    28. textTracks
    29. videoTracks
    30. volume
  3. 方法
    1. canPlayType()
    2. captureStream()
    3. fastSeek()
    4. load()
    5. msInsertAudioEffect()
    6. pause()
    7. play()
    8. seekToNextFrame()
    9. setMediaKeys()
    10. setSinkId()
  4. 事件
    1. abort
    2. canplay
    3. canplaythrough
    4. durationchange
    5. emptied
    6. ended
    7. error
    8. loadeddata
    9. loadedmetadata
    10. loadstart
    11. pause
    12. play
    13. progress
    14. ratechange
    15. seeked
    16. seeking
    17. stalled
    18. suspend
    19. timeupdate
    20. volumechange
    21. waiting
  5. 继承:
    1. HTMLElement
    2. 元素
    3. 节点
    4. EventTarget
  6. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMetaElement
    42. HTMLMeterElement
    43. HTMLModElement
    44. HTMLOListElement
    45. HTMLObjectElement
    46. HTMLOptGroupElement
    47. HTMLOptionElement
    48. HTMLOptionsCollection
    49. HTMLOutputElement
    50. HTMLParagraphElement
    51. HTMLParamElement
    52. HTMLPictureElement
    53. HTMLPreElement
    54. HTMLProgressElement
    55. HTMLQuoteElement
    56. HTMLScriptElement
    57. HTMLSelectElement
    58. HTMLShadowElement
    59. HTMLSourceElement
    60. HTMLSpanElement
    61. HTMLStyleElement
    62. HTMLTableCaptionElement
    63. HTMLTableCellElement
    64. HTMLTableColElement
    65. HTMLTableDataCellElement
    66. HTMLTableElement
    67. HTMLTableHeaderCellElement
    68. HTMLTableRowElement
    69. HTMLTableSectionElement
    70. HTMLTemplateElement
    71. HTMLTextAreaElement
    72. HTMLTimeElement
    73. HTMLTitleElement
    74. HTMLTrackElement
    75. HTMLUListElement
    76. HTMLUnknownElement
    77. HTMLVideoElement
    78. HashChangeEvent
    79. 历史
    80. ImageData
    81. 定位
    82. MessageChannel
    83. MessageEvent
    84. MessagePort
    85. Navigator
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1