-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add a ManagedMediaSource demo #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # ManagedMediaSource demo | ||
|
|
||
| This example demonstrates how to use the [ManagedMediaSource](https://developer.mozilla.org/en-US/docs/Web/API/ManagedMediaSource) interface to set up a managed media source, attach it to a video element, and respond to `startstreaming` and `endstreaming` events to control when media data is fetched. It also listens for `bufferedchange` events on the `ManagedSourceBuffer` and logs the added time ranges. | ||
|
|
||
| [See the example live](https://mdn.github.io/dom-examples/media-source-extensions/managed-media-source/). | ||
103 changes: 103 additions & 0 deletions
103
media-source-extensions/managed-media-source/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>ManagedMediaSource demo</title> | ||
| <style> | ||
| body { | ||
| font-family: system-ui, sans-serif; | ||
| max-width: 460px; | ||
| margin: 1rem auto; | ||
| padding: 0 1rem; | ||
| } | ||
|
|
||
| video { | ||
| display: block; | ||
| width: 100%; | ||
| margin-bottom: 1rem; | ||
| background: #000; | ||
| } | ||
|
|
||
| output { | ||
| display: block; | ||
| white-space: pre; | ||
| height: 6rem; | ||
| overflow-y: auto; | ||
| border: 1px solid #ccc; | ||
| border-radius: 4px; | ||
| padding: 0.5rem; | ||
| font-family: monospace; | ||
| font-size: 0.85rem; | ||
| line-height: 1.4; | ||
| } | ||
|
|
||
| .not-supported { | ||
| padding: 1rem; | ||
| background: #fff3cd; | ||
| border: 1px solid #ffc107; | ||
| border-radius: 4px; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <h1>ManagedMediaSource demo</h1> | ||
| <video controls width="400" height="225"></video> | ||
| <output id="log"></output> | ||
|
|
||
| <!-- | ||
| Used in: | ||
| - https://developer.mozilla.org/en-US/docs/Web/API/ManagedMediaSource | ||
| --> | ||
|
|
||
| <script> | ||
| const output = document.querySelector("#log"); | ||
| function log(msg) { | ||
| output.textContent += msg + "\n"; | ||
| output.scrollTop = output.scrollHeight; | ||
| } | ||
|
|
||
| const videoUrl = | ||
| "https://mdn.github.io/shared-assets/videos/flower-fragmented.mp4"; | ||
| const mediaType = 'video/mp4; codecs="avc1.64001F, mp4a.40.2"'; | ||
| const video = document.querySelector("video"); | ||
|
|
||
| if (!window.ManagedMediaSource?.isTypeSupported(mediaType)) { | ||
| log("ManagedMediaSource is not supported in this browser."); | ||
| document.querySelector("video").style.display = "none"; | ||
| output.classList.add("not-supported"); | ||
| } else { | ||
| const source = new ManagedMediaSource(); | ||
| video.disableRemotePlayback = true; | ||
| video.src = URL.createObjectURL(source); | ||
|
|
||
| video.addEventListener("canplay", () => | ||
| log("canplay — video is ready"), | ||
| ); | ||
|
|
||
| source.addEventListener("sourceopen", () => { | ||
| const sourceBuffer = source.addSourceBuffer(mediaType); | ||
|
|
||
| sourceBuffer.addEventListener("bufferedchange", (event) => { | ||
| for (let i = 0; i < event.addedRanges.length; i++) { | ||
| log( | ||
| `Buffered: ${event.addedRanges.start(i).toFixed(2)}s – ${event.addedRanges.end(i).toFixed(2)}s`, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| source.addEventListener("startstreaming", async () => { | ||
| log("startstreaming — fetching media data…"); | ||
| const response = await fetch(videoUrl); | ||
| const data = await response.arrayBuffer(); | ||
| sourceBuffer.appendBuffer(data); | ||
| }); | ||
|
|
||
| source.addEventListener("endstreaming", () => { | ||
| log("endstreaming — enough data buffered"); | ||
| }); | ||
| }); | ||
| } | ||
| </script> | ||
| </body> | ||
| </html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.