youtube-play
v5.1.0
Published
A friendly wrapper around the YouTube Iframe API.
Maintainers
Readme
youtube-play
A friendly wrapper around the YouTube Iframe API.
:cloud: Installation
Check out the dist directory to download the needed files and include them on your page.
If you're using this module in a CommonJS environment, you can install it using npm or yarn and require it:
# Using npm
npm install --save youtube-play
# Using yarn
yarn add youtube-play:clipboard: Example
const ytPlayer = require("youtube-play");
console.log(ytPlayer());:memo: Documentation
YTPlayer
A friendly wrapper around the YouTube Iframe API.
Params
- string
id: The ID of the container element or the element itself. - Object
options: Configuration options for the player.
_onIframeApiLoadError(err)
Handle errors that occur while loading the YouTube Iframe API.
Params
- Error
err: The error that occurred.
Return
- void
getIdFromURL(url)
Extract the YouTube video ID from a URL.
Params
- string
url: The YouTube video URL.
Return
- string|boolean The video ID if valid (11 characters), false otherwise.
_loadIframeApi()
Load the YouTube Iframe API script and set up the YouTube ready callback.
Return
- void
onYTReady(cb)
Register a callback to be invoked when the YouTube Iframe API is ready.
Params
- Function
cb: The callback function to invoke when the API is ready.
Return
- void
constructor(id, options, [options.events={}], [options.handleFullscreen=false], [options.fullscreenIntervalCheck=500])
Initialize a new YTPlayer instance.
Params
- string|HTMLElement
id: The ID of the container element or the element itself. - Object
options: Configuration options for the player. - Object
[options.events={}]: Event handlers to attach to the player. - boolean
[options.handleFullscreen=false]: Whether to handle fullscreen changes. - number
[options.fullscreenIntervalCheck=500]: Interval in ms for fullscreen checks.
Return
- void
_onStateChange(e, data)
Handle YouTube player state change events.
Params
- Object
e: The YouTube state change event object. - number
data: The player state code.
Return
- void
on(ev, cb)
Register an event listener on the player.
Params
- string
ev: The event name to listen for. - Function
cb: The callback function to invoke when the event fires.
Return
- YTPlayer This instance for chaining.
off(ev, cb)
Remove an event listener from the player.
Params
- string
ev: The event name to stop listening for. - Function
cb: The callback function to remove.
Return
- YTPlayer The result of the parent removeListener method.
playVideo()
Play the video (schedules play on Android/iOS after ready).
Return
- YTPlayer This instance for chaining.
ready(cb)
Register a callback to be invoked when the player is ready.
Params
- Function
cb: The callback function to invoke when the player is ready.
Return
- YTPlayer This instance for chaining.
destroy()
Destroy the player and clean up all event listeners.
Return
- YTPlayer This instance for chaining.
safePlay()
Safely play the video with retry logic, buffering detection, and error handling.
Return
- Promise<YTPlayer> A promise that resolves to this instance when playback succeeds, or rejects with an error.
:question: Get Help
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues. :bug:
- For direct and quick help, you can use Codementor. :rocket:
:yum: How to contribute
Have an idea? Found a bug? See how to contribute.
:sparkling_heart: Support my projects
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
Starring and sharing the projects you like :rocket:
—I love books! I will remember you after years if you buy me one. :grin: :book:
—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea:
—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
Bitcoin—You can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks! :heart:
