API wrapper for Youtube, Vimeo and Self-Hosted videos
npm install video-workerAPI wrapper for Youtube, Vimeo and Self-Hosted videos
- Import VideoWorker
- ESM
- ESM CDN
- UMD
- UMD CDN
- CJS (Bundlers like Webpack)
- Use VideoWorker
- Options
- Events
- Methods
- Video Providers
- For Developers
Use one of the following examples to import script.
We provide a version of VideoWorker built as ESM (video-worker.esm.js and video-worker.esm.min.js) which allows you to use VideoWorker as a module in your browser, if your targeted browsers support it.
``html`
`html`
VideoWorker may be also used in a traditional way by including script in HTML and using library by accessing window.VideoWorker.
`html`
`html`
Install VideoWorker as a Node.js module using npm
``
npm install video-worker
Import VideoWorker by adding this line to your app's entry point (usually index.js or app.js):
`javascript`
import VideoWorker from 'video-worker';
`javascript
import VideoWorker from 'video-worker';
const videoObject = new VideoWorker('https://www.youtube.com/watch?v=ab0TSkLe-E0');
if (videoObject.isValid()) {
// retrieve iframe/video dom element.
videoObject.getVideo((video) => {
const $parent = video.parentNode;
// insert video in the body.
document.body.appendChild(video);
// remove temporary parent video element (created by VideoWorker).
$parent.parentNode.removeChild($parent);
});
}
`
Video URLs examples:
* YouTube https://www.youtube.com/watch?v=ab0TSkLe-E0https://vimeo.com/110138539
* Vimeo mp4:./self-hosted-video.mp4,webm:./self-hosted-video.webm,ogv:./self-hosted-video.ogv
* Self Hosted
Note: for self-hosted videos required only 1 video type, not necessary use all mp4, webm and ogv. This need only for maximum compatibility with all browsers.
Name | Type | Default | Description
:--- | :--- | :------ | :----------
autoplay | bool | false | Video autoplay.false
loop | bool | | Video playing loop.true
showControls | bool | | Video controls.false
accessibilityHidden | bool | | Add accessibility attributes for videos used on backgrounds.false
mute | bool | | Mute sound.100
volume | int | | Volume level from 0 to 100.0
startTime | float | | Start time in seconds when video will be started (this value will be applied also after loop).0
endTime | float | | End time in seconds when video will be ended.
`javascript`
new VideoWorker('
autoplay: true,
loop: true,
startTime: 10,
});
Name | Parameters | Description
:--- | :----- | :----------
ready | event | Fires only once, when the video is ready to play.event
volumechange | | Fires when video volume changed.event
timeupdate | | Fires when video current time changed.event
started | | Fires only once, when the video is started playing.event
play | | Fires on video play start.event
pause | | Fires on video paused.event
ended | | Fires on video ended.error
error | | Fires on video error
`javascript`
videoObject.on('ready', (event) => {
console.log('video ready', event);
});
Name | Result | Description
:--- | :----- | :----------
isValid | bool | Check if the video is successfully determined and ready to use.
play | - | Play video.
pause | - | Pause video.
mute | - | Mute sound.
unmute | - | Unmute sound.
getMuted | int | Get mute state. videoObject.getMuted((muted) => { ... })videoObject.setVolume(40);
setVolume | - | Set volume level (takes integer value from 0 to 100). videoObject.getVolume((volume) => { ... })
getVolume | int | Get volume level. videoObject.setCurrentTime(40);
setCurrentTime | - | Set current time in seconds. videoObject.getCurrentTime((currentTime) => { ... })
getCurrentTime | int | Get current time in seconds. videoObject.getImageURL((url) => { ... })
getImageURL | string | Retrieves Youtube/Vimeo video poster image URL. videoObject.getVideo((video) => { ... })
getVideo | dom | Retrieves iframe/video dom element.
`javascript`
videoObject.mute();
By default VideoWorker provides support for Youtube, Vimeo and Self-Hosted videos. There is a possibility to extend providers list and add support for different platform.
Example:
`javascript`
VideoWorker.providers.MyVideoProvider = class MyVideoProvider extends VideoWorker.BaseClass {
type = 'my-video-provider';
...
}
All available methods for custom provider class you can find in existing providers -
* Run npm install in the command line. Or if you need to update some dependencies, run npm update
* npm run build to run build
* npm run js-lint to show eslint errorsnpm run js-lint-fix` to automatically fix some of the eslint errors
*