Keeping track of time
Inspect the current playhead and the ranges that describe media playback.
Tip: open your browser's developer console and use the player and THEOplayer variables to interact with this demo.
| Current time | 0:00.0 |
|---|---|
| Duration | 0:00.0 |
| Paused | Yes |
Played
Buffered
Seekable
TimeRanges can contain zero or more intervals. Listen for playback and buffering events to keep a readout current.
See the TimeRanges API.
function updateRanges() {
console.log(player.currentTime, player.duration, player.played, player.buffered, player.seekable);
}
player.addEventListener(['timeupdate', 'progress', 'durationchange', 'seeking', 'seeked', 'play', 'pause'], updateRanges);
Code
demo.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Keeping track of time</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet" />
<style>
html,
body {
margin: 0;
padding: 0;
}
theoplayer-default-ui:not(:defined) {
display: inline-block;
box-sizing: border-box;
}
theoplayer-default-ui {
width: 100%;
aspect-ratio: 16 / 9;
font-family: 'Noto Sans', sans-serif;
background: #000;
}
</style>
<script src="https://cdn.theoplayer.com/dash/theoplayer/THEOplayer.js"></script>
<script nomodule src="https://unpkg.com/@theoplayer/web-ui@2/polyfills"></script>
<script async src="https://unpkg.com/@theoplayer/web-ui@2"></script>
</head>
<body>
<!--
libraryLocation: For demonstration purposes, we use the THEOplayer CDN.
For production use, host the library yourself and update this URL and the script above.
licenseUrl: Point this to your license file, or replace it with a license property.
-->
<theoplayer-default-ui
configuration='{"libraryLocation":"https://cdn.theoplayer.com/dash/theoplayer/","licenseUrl":"../../../../theoplayer-license.txt"}'
source='{"sources":{"src":"https://cdn.theoplayer.com/video/big_buck_bunny/big_buck_bunny_metadata.m3u8","type":"application/x-mpegurl"},"poster":"https://cdn.theoplayer.com/video/big_buck_bunny/poster.jpg","metadata":{"title":"Big Buck Bunny"}}'
></theoplayer-default-ui>
<script src="../utils.js"></script>
<script>
onPlayerReady((player) => {
function ranges(timeRanges) {
const result = [];
for (let index = 0; index < timeRanges.length; index += 1) {
result.push({ start: timeRanges.start(index), end: timeRanges.end(index) });
}
return result;
}
function report() {
sendToParent('time-ranges', {
currentTime: player.currentTime,
duration: player.duration,
paused: player.paused,
played: ranges(player.played),
buffered: ranges(player.buffered),
seekable: ranges(player.seekable),
});
}
player.addEventListener(['timeupdate', 'progress', 'durationchange', 'seeking', 'seeked', 'play', 'pause'], report);
report();
});
</script>
</body>
</html>