百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

youtube-player

> 开发工具
开源

YouTube iframe API 抽象。

386 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

YouTube iframe API 抽象。

YouTube Player

  • Usage
    • Events
  • Polyfills
  • Examples
  • Debugging
  • Download
  • Running the Examples

youtube-player is an abstraction of YouTube IFrame Player API (YIPA).

The downsides of using YouTube IFrame Player API are:

  • Requires to define callbacks in the global scope (window).
  • Requires to track the state of a player (e.g. you must ensure that video player is "ready" before you can use the API).

youtube-player:

  • Registers listeners required to establish when YIPA has been loaded.
  • Does not overwrite global YIPA callback functions.
  • Queues player API calls until when video player is "ready".

Usage

…

youtube-player is a factory function.

The resulting object exposes all functions of an instance of YT.Player. The difference is that the function body is wrapped in a promise. This promise is resolved only when the player has finished loading and is ready to begin receiving API calls (onReady). Therefore, all function calls are queued and replayed only when player is ready.

This encapsulation does not affect the API other than making every function return a promise.

javascript
let player;

player = YouTubePlayer('video-player');

// 'loadVideoById' is queued until the player is ready to receive API calls.
player.loadVideoById('M7lc1UVf-VE');

// 'playVideo' is queue until the player is ready to received API calls and after 'loadVideoById' has been called.
player.playVideo();

// 'stopVideo' is queued after 'playVideo'.
player
    .stopVideo()
    .then(() => {
        // Every function returns a promise that is resolved after the target function has been executed.
    });

Events

player.on event emitter is used to listen to all YouTube IFrame Player API events, e.g.

javascript
player.on('stateChange', (event) => {
    // event.data
});

player.off removes a previously added event listener, e.g.

javascript
var listener = player.on(/* ... */);

player.off(listener);

Polyfills

Note that the built version does not inline polyfills.

You need to polyfill the environment locally (e.g. using a service such as https://polyfill.io/v2/docs/).

Examples

  • Playing a video.
  • Multiple players.
  • Registering events handlers.

Debugging

youtube-player is using debug module to expose debugging information.

The debug namespace is "youtube-player".

To display youtube-player logs configure localStorage.debug, e.g.

javascript
localStorage.debug = 'youtube-player:*';

Download

Using NPM:

bash
npm install youtube-player

Running the Examples

bash
npm install
npm run build
cd ./examples
npm install
npm run start

This will start a HTTP server on port 8000.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptapi-sdkvideo-playeryoutube

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具