1.0.6 • Published 1 year ago
@42videobricks/42videobricks-playeriframe v1.0.6
42Videobricks Player
A lightweight JavaScript library for controlling 42Videobricks video player iframes. This library provides a simple interface for video playback control, event handling, and player state management.
Installation
npm install @42videobricks/42videobricks-playeriframe@latestOr via CDN:
<script src="https://unpkg.com/@42videobricks/42videobricks-playeriframe/dist/index.min.js"></script>Quick Start
<iframe
id="myPlayer"
src="https://stream.42videobricks.com/WWhUSEJqbHRLeVR5MlVPOA==/player"
width="640"
height="360"
allow="autoplay; fullscreen"
allowfullscreen>
</iframe>
<script>
const iframe = document.getElementById('myPlayer');
const player = new Api42Videobricks(iframe);
player.onReady(() => {
player.play();
});
</script>Usage
Initialize Player
const player = new Api42Videobricks(iframeElement, {
origin: '*' // Use '*' for development
});Basic Controls
// Playback
player.play();
player.pause();
// Volume
player.mute();
player.unmute();
player.setSound(0.5); // 0 to 1
// Navigation
player.rewind(10); // Rewind 10 seconds
player.forward(10); // Forward 10 seconds
// Target Times
player.addTargetTime(30, { label: 'Chapitre 1' }); // Add a target time at 30 seconds
player.removeTargetTime(30); // Remove a specific target time
player.clearTargetTimes(); // Remove all target times
// Fullscreen if the user interacted with the player beforehand
player.enterFullscreen();
player.leaveFullscreen();Event Handling
player.onReady(() => {
console.log('Player is ready');
});
player.on('videoPlaying', () => {
console.log('Video started playing');
});
player.on('videoPaused', () => {
console.log('Video paused');
});
player.on('videoEnded', () => {
console.log('Video ended');
});Async Methods
async function getPlayerInfo() {
const videoInfo = await player.getVideoInfos();
const currentTime = await player.getCurrentTime();
const quality = await player.getQuality();
const volume = await player.getSound();
}API Reference
Constructor
new Api42Videobricks(element, options)element: HTMLIFrameElementoptions: Objectorigin: string (default: '*')
Methods
Player Controls
play(): Start playbackpause(): Pause playbackmute(): Mute audiounmute(): Unmute audiosetSound(value): Set volume (0-1)setLoop(value): Set loop stateenterFullscreen(): Enter fullscreenleaveFullscreen(): Exit fullscreenrewind(duration): Rewind videoforward(duration): Forward videoaddTargetTime(time, data): Add a target time with optional dataremoveTargetTime(time): Remove a specific target timeclearTargetTimes(): Remove all target times
Event Handling
onReady(callback): Register ready callbackon(eventName, callback): Add event listeneroff(eventName, callback): Remove event listener
Async Getters
getVideoInfos()getCurrentTime()getLoop()getSound()getQuality()getQualities()
Events
canPlay: Video can playingerror: Video on errorqualityChange: Quality changestimeUpdate: Playback position changesvideoEnded: Video playback endsvideoPaused: Video is pausedvideoPlaying: Video start playingvolumeChange: Volume changes
Known Limitations
Autoplay & Unmute
On some browsers (like Chrome), trying to unmute (unmute()) without user interaction can automatically pause the video. Refer to the official documentation for more details.
Fullscreen
Entering fullscreen mode (enterFullscreen()) requires an explicit user gesture (e.g., click or key press). Otherwise, the following error will be thrown:Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user gesture.
Browser Support
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
License
MIT © Alchimie