youtube-player vs react-youtube
YouTube Video Player Libraries Comparison
1 Year
youtube-playerreact-youtubeSimilar Packages:
What's YouTube Video Player Libraries?

Both 'react-youtube' and 'youtube-player' are libraries designed to facilitate the integration of YouTube video playback into web applications. 'react-youtube' is specifically tailored for React applications, providing a declarative way to embed YouTube videos using React components. It offers a simple API for controlling video playback, handling events, and managing video states. On the other hand, 'youtube-player' is a more general-purpose library that can be used with any JavaScript framework or vanilla JavaScript. It provides a more low-level API for controlling YouTube videos, allowing for greater customization and flexibility in how videos are embedded and controlled.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
youtube-player501,28138032.8 kB242 years agoBSD-3-Clause
react-youtube443,1771,88174.2 kB83-MIT
Feature Comparison: youtube-player vs react-youtube

Integration with Frameworks

  • youtube-player:

    'youtube-player' is framework-agnostic, meaning it can be used with any JavaScript framework or even with vanilla JavaScript. This makes it a versatile choice for developers who may not be working within a specific framework.

  • react-youtube:

    Designed specifically for React, 'react-youtube' allows developers to easily integrate YouTube videos using React components. It leverages React's lifecycle methods and hooks, making it seamless to manage video states and events in a React application.

API and Control

  • youtube-player:

    Offers a more extensive and low-level API that allows for detailed control over the YouTube player. Developers can manipulate the player in various ways, such as changing playback rates, handling buffering, and responding to a wider range of player events.

  • react-youtube:

    Provides a simple and declarative API for controlling video playback, including methods for play, pause, and seeking. It also supports event handling for actions like video state changes, making it easy to respond to user interactions within a React app.

Customization

  • youtube-player:

    Highly customizable, allowing developers to modify player settings, styles, and behaviors. This flexibility is beneficial for applications that require a tailored video playback experience.

  • react-youtube:

    Customization options are somewhat limited to the properties and methods exposed by the React component. It is designed to be straightforward and easy to use, focusing on React's declarative nature rather than extensive customization.

Learning Curve

  • youtube-player:

    May have a steeper learning curve for those unfamiliar with JavaScript or the YouTube Player API. Developers need to understand how to interact with the player through its API, which can be more complex than using a React component.

  • react-youtube:

    Generally easier for developers already familiar with React, as it follows React's paradigms and practices. The learning curve is minimal for those who understand React components and state management.

Event Handling

  • youtube-player:

    Provides a broader range of events that can be listened to, such as buffering, state changes, and errors. This allows for more complex event handling scenarios but requires more manual setup compared to the React-specific approach.

  • react-youtube:

    Utilizes React's event handling system, allowing developers to easily manage events like video play, pause, and end within the React component lifecycle. This integration makes it intuitive for React developers to handle video-related events.

How to Choose: youtube-player vs react-youtube
  • youtube-player:

    Choose 'youtube-player' if you need a more flexible solution that can be integrated into various JavaScript frameworks or if you require advanced customization options for video playback. This library is suitable for projects that may not be using React or where a more granular control over the YouTube player is necessary.

  • react-youtube:

    Choose 'react-youtube' if you are building a React application and prefer a component-based approach to embed YouTube videos. It simplifies the integration process by providing React-specific features and hooks, making it easier to manage video states and events in a React-friendly way.

README for youtube-player

YouTube Player

Travis build status NPM version Canonical Code Style Twitter Follow

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

/**
 * @typedef options
 * @see https://developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player
 * @param {Number} width
 * @param {Number} height
 * @param {String} videoId
 * @param {Object} playerVars
 * @param {Object} events
 */

/**
 * @typedef YT.Player
 * @see https://developers.google.com/youtube/iframe_api_reference
 * */

/**
 * A factory function used to produce an instance of YT.Player and queue function calls and proxy events of the resulting object.
 *
 * @param {YT.Player|HTMLElement|String} elementId Either An existing YT.Player instance,
 * the DOM element or the id of the HTML element where the API will insert an <iframe>.
 * @param {YouTubePlayer~options} options See `options` (Ignored when using an existing YT.Player instance).
 * @param {boolean} strictState A flag designating whether or not to wait for
 * an acceptable state when calling supported functions. Default: `false`.
 * See `FunctionStateMap.js` for supported functions and acceptable states.
 * @returns {Object}
 */
import YouTubePlayer from 'youtube-player';

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.

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.

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

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

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

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.

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

Download

Using NPM:

npm install youtube-player

Running the Examples

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

This will start a HTTP server on port 8000.