Building a custom chapters navigation
The main function used here is player.api.getMetaData(). This function allows you to read the metadata associated with the video. The metadata includes the chapters information which is used to render the menu below the player.
- index.html
- script.js
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Chapters Demo</title>
<style>
main {
font-family: 'Helvetica Neue', Ubuntu, Arial, sans-serif;
}
#chapter-box li {
cursor: pointer;
background-color: #f9f9f9;
list-style-type: disclosure-closed;
padding: 5px 15px;
}
#chapter-box li:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<script src="https://player.vdocipher.com/v2/api.js"></script>
<main>
<iframe
src="https://player.vdocipher.com/v2/?otp=20160313versUSE313w8bMFK2Yt8GxDgRTUdX7tRSOYDpfTY0PROTiHxZIamHxKY&playbackInfo=eyJ2aWRlb0lkIjoiYWU0ZDJhYjIyMjIwNGNkY2E2YTY3MzZhM2Y3Y2UxNTkifQ=="
style="border: 0; height: 360px; width: 640px; max-width: 100%"
allowfullscreen
allow="encrypted-media"
></iframe>
<h4>Chapters</h4>
<ul id="chapter-box"></ul>
</main>
<script src="./script.js"></script>
</body>
</html>
const iframe = document.querySelector('iframe');
const chapterBox = document.querySelector('#chapter-box');
const player = VdoPlayer.getInstance(iframe);
(async function () {
const meta = await player.api.getMetaData();
meta.chapters.forEach(({title, startTime}) => {
const chapterLine = document.createElement('li');
chapterLine.innerText = title;
chapterLine.addEventListener('click', () => {
player.video.currentTime = startTime;
player.video.play();
});
chapterBox.appendChild(chapterLine);
});
})();