Hi there! I’m trying to understand the player API:
And I wrote some simple code to test it.
Basically, I want to log when a video changes status (playing/paused/ended) and while it’s playing (ONLY while it’s playing), I want to log the position in the video.
So, I followed the instructions in the guide, but I’m getting some odd bugs. Here’s my code (it’s a .html file):
<!DOCTYPE html>
<html>
<script src="https://unpkg.com/@peertube/embed-api/build/player.min.js"></script>
<h1>PeerTube test</h1>
<iframe id="player" title=""Цапля и журавль" © Союзмультфильм, 1974 г. Советский мультфильм для детей. Смотреть онлайн." src="https://p2ptv.ru/videos/embed/4be9a87a-5081-4ebd-b244-5b1928b104dd?api=1" allowfullscreen="" sandbox="allow-same-origin allow-scripts allow-popups" width="480" height="360" frameborder="0"></iframe>
<script>
// div with class vjs-poster has the preview thumbnail image.
// https://p2ptv.ru/lazy-static/previews/55dcac25-61f6-4702-b240-a8d3571fcf1a.jpg
const PeerTubePlayer = window['PeerTubePlayer'];
let player = new PeerTubePlayer(document.querySelector('#player'));
const logStatus = (e) => console.log(e);
const initialize = async () => {
await player.ready; // wait for the player to be ready
console.log({player});
// now you can use it!
player.addEventListener('playbackStatusChange', (e) => {
logStatus(e); // either "playing" or "paused" or "ended"
if (e === 'playing') {
player.addEventListener('playbackStatusUpdate', logStatus);
} else {
console.log('listener removed');
player.removeEventListener('playbackStatusUpdate', logStatus);
}
});
}
initialize();
</script>
</html>
It works in that the video loads, it logs the video state changes, and it starts logging the detailed video info every half-second after I click play for the first time.
But there are some bugs.
First of all, I’m getting the following 3 errors in the console:
SecurityError: Permission denied to access property "document" on cross-origin object [content-script.js:28](moz-extension://1b86d58b-56fc-4371-8a3c-8975f6759c46/content-script.js)
Cannot import or register plugin web-monetization. Error: This helper is not implemented in embed.
HLS.js error: mediaError - fatal: false - bufferStalledError
Object { type: "mediaError", details: "bufferStalledError", fatal: false, buffer: 8.122643 }
[586.8cd713c8565c96e0b342.chunk.js:1:3157](https://p2ptv.ru/client/standalone/videos/586.8cd713c8565c96e0b342.chunk.js)
Also, logStatus() keeps firing even after removeEventListener() is called - if the video is paused or ended, it STILL logs the video info every half second.
Can anyone please help?
One more question - is there any way to easily get the video thumbnail image, given a particular video URL or embed URL?