Skip to content
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

feat: enable media element reuse #345

Merged
merged 8 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
60 changes: 55 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
}

.box {
display: flex;
gap: 1rem;
margin: 0 0 1rem 0;
}

.box-2 {
display: flex;
flex-direction: column;
gap: 1rem;
margin: 0 0 1rem 0;
}

Expand All @@ -44,18 +53,37 @@
background: rgba(255, 198, 109, 1);
}

button {
color: #1a1a1a;
padding: 0.2rem 1rem;
margin-bottom: 0.2rem;
border-radius: 0.2rem;
font-size: 1.2rem;
line-height: 1.8;
background: rgba(255, 198, 109, 1);
border: 2px solid rgb(173, 131, 68);
font-weight: bold;
text-transform: uppercase;
cursor: pointer;

&:hover {
transform: scale(1.1);
}
}

#events {
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box">
<video
controls
src="https://testcontent.eyevinn.technology/mp4/VINN.mp4"
id="videoEl"
></video>
<video preload="metadata" playsinline controls id="videoEl"></video>
<div class="box-2">
<button id="switch">Update src</button>
<button id="clear">Clear src</button>
<button id="load">Load</button>
</div>
</div>
<p>Check console for additional logging.</p>
<div class="box">
Expand All @@ -70,7 +98,13 @@
MediaEvent,
} from "./main.mjs";

const videos = [
"https://testcontent.eyevinn.technology/mp4/VINN.mp4",
"https://testcontent.eyevinn.technology/mp4/stswe-tvplus-promo.mp4",
];

const mediaElement = document.getElementById("videoEl");
mediaElement.src = videos[0];
let i = 1;

Object.values(MediaEvent).forEach((evt) =>
Expand Down Expand Up @@ -101,5 +135,21 @@
}
},
});

const switchButton = document.getElementById("switch");
const clearButton = document.getElementById("clear");
const loadButton = document.getElementById("load");

switchButton.onclick = () => {
mediaElement.src = mediaElement.src !== videos[0] ? videos[0] : videos[1];
};

clearButton.onclick = () => {
mediaElement.src = "";
};

loadButton.onclick = () => {
mediaElement.load();
};
</script>
</body>
11 changes: 11 additions & 0 deletions src/media-event-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ export const getMediaEventFilter = ({
const isNotReady = (): boolean =>
allowResumeAfterEnded ? state.loading : state.loading || state.ended;

// Reset state of media event filter when a new src is
// attached to the media element.
const onEmptied = (): void => {
clearRatechangeBufferTimeout();

state = {
...initialState,
};
};

const onCanPlayThrough = (): void => {
if (!state.loading) {
// Recover from Safari "mute" micro buffer triggered by "waiting"
Expand Down Expand Up @@ -455,6 +465,7 @@ export const getMediaEventFilter = ({
[MediaEvent.play, onPlay],
[MediaEvent.pause, onPause],
[MediaEvent.ratechange, onRatechange],
[MediaEvent.emptied, onEmptied],
];

EventHandlerPairs.forEach(([event, handler]) =>
Expand Down
Loading