addSourceBuffer method
Creates a new SourceBuffer and adds it to the SourceBuffers property of the MediaSource.
![]() |
Syntax
var sourcebuffer = MediaSource.addSourceBuffer(type);Parameters
- type [in]
-
Type: DOMString
The MIME type. This is expressed typically as
'video/mp4'or as a MIME type and a codec:'video/mp4;codecs=avc1.4d0020,mp4a.40.2'. Internet Explorer accepts both formats, though other browsers may require the codec be included.
Return value
Type: SourceBuffer
The media source buffer.
Exceptions
| Exception | Condition |
|---|---|
|
If type is null or an empty string. |
|
If type contains a MIME type that's not supported or a MIME type that's not supported by SourceBuffer. |
|
If the mediaSource can't handle any more SourceBuffer objects. |
Examples
This example gets a video object, creates a new MediaSource object, and assigns the MediaSource object to the src (source) of the video object. It then waits for the sourceopen event to fire, and then creates a video SourceBuffer using addSourceBuffer.
// Create mediaSource and initialize video
function setupVideo() {
clearLog(); // Clear console log
// Create the media source
if (window.MediaSource) {
mediaSource = new window.MediaSource();
} else {
log("mediasource or syntax not supported");
return;
}
var url = URL.createObjectURL(mediaSource);
videoElement.pause();
videoElement.src = url;
videoElement.width = width;
videoElement.height = height;
// Wait for event that tells us that our media source object is
// ready for a buffer to be added.
mediaSource.addEventListener('sourceopen', function (e) {
try {
videoSource = mediaSource.addSourceBuffer('video/mp4');
initVideo(initialization, file);
} catch (e) {
log('Exception calling addSourceBuffer for video', e);
return;
}
},false);
See also
