NPlayer Documentation
repository·main·Indexed 23 days ago
https://github.com/oyuyue/nplayerA highly customizable, dependency-free video player written in TypeScript and Sass. NPlayer supports mobile, SSR, live streaming, and media formats including HLS, DASH, and FLV, with compatibility back to IE11. It features a plugin system for extended functionality, including a dedicated danmaku (bullet comment) plugin, and provides official integrations for React and Vue 2/3.
What's inside NPlayer
- NPlayer is a powerful danmaku video player. It provides a core player engine and a suite of ecosystem plugins for different environments, including React and Vue, as well as a dedicated danmaku plugin.
Implement an NPlayer plugin
mainNPlayer plugins are objects used to extend player functionality. A plugin must implement an
applymethod which receives theplayerinstance. Optionally, you can implement adisposemethod to handle cleanup when the player is destroyed.To use a plugin, pass an array of plugin objects to the
pluginsproperty in thePlayerconstructor.new Player({ plugins: [ { apply(player) { player.on('Mounted', () => console.log('mounted')) player.on('Play', () => console.log('play')) } } ] })NPlayer Ecosystem and Plugins
mainNPlayer features a plugin system (e.g., for Danmaku/bullet comments) and provides official integrations for modern frameworks. Use these packages to add specific functionality or integrate with your existing UI library:
- @nplayer/danmaku: Danmaku (bullet comment) plugin.
- @nplayer/react: React integration.
- @nplayer/vue: Vue 2 and Vue 3 integration.
Configure multiple control bars
mainSince
controlsis a 2D array, you can distribute items across the three available bars (bottom 1, bottom 2, and top). The array index0is the bottom-most bar, and index2is the top-most bar.Note on Singletons: Control items are singletons. If you include the same ID (e.g.,
'settings') in two different bars, the item will move to the last specified location rather than appearing twice. Thespaceris an exception: it can appear in multiple bars, but only once per bar.Enable IE 11 compatibility using polyfills
mainIf you must support IE 11 directly, you need to provide polyfills for both JavaScript and CSS features that IE 11 does not support.
JavaScript Compatibility: Include the NPlayer polyfill file before initializing NPlayer:
https://github.com/woopen/nplayer/blob/main/fixtures/polyfill.jsCSS Compatibility: NPlayer uses CSS variables which are unsupported in IE 11. Use the
ie11CustomPropertiespolyfill to resolve these issues.
Access and use NPlayer built-in components
mainNPlayer provides several built-in components for consistent UI interaction and secondary development. You can access these components via the
componentsproperty on thePlayerclass or by importing them directly from thenplayerpackage.All built-in components share a common interface:
- The first argument of the
constructoris the container element (HTMLElement). - They all have an
elproperty representing their DOM element. - They all have a
dispose()method to destroy the component.
import Player, { Tooltip } from 'nplayer' // Accessing via Player class console.log(Tooltip === Player.components.Tooltip) // true- The first argument of the
Configure the video poster
mainYou can add a poster image to your video by providing a URL to the
posterparameter in thePlayerconstructor.To enable or disable the poster functionality, use the
posterEnableparameter (defaults totrue).Additional customization options include:
posterPlayEl: Customizes the play button element.posterBgColor: Customizes the poster background color (defaults to transparent).
For detailed styling instructions, refer to the Customizing Themes documentation.
new Player({ poster: 'http://image.jpg', posterEnable: true, // posterPlayEl // posterBgColor })Setup NPlayer plugin in Vue 3
mainTo use the NPlayer component in a Vue 3 application, register the plugin using the
.use()method on your app instance.import { createApp } from "vue"; import NPlayer from "@nplayer/vue"; import App from "./App.vue"; createApp(App).use(NPlayer).mount("#app");Use NPlayer built-in components and themes in plugins
mainBuilt-in Components
Access NPlayer's built-in components within your plugin via
player.Player.components.Theming
To ensure your plugin UI matches the player's theme, use CSS variables. All plugin CSS classes should be prefixed with
.nplayer_to maintain consistency.Example CSS usage:
.nplayer_my_plugin { color: var(--theme-color); }Install NPlayer via npm or yarn
mainYou can install NPlayer as a dependency using
npmoryarn. Once installed, import thePlayerclass and call.mount(selector)to attach it to a DOM element.Note: You do not need to import a separate CSS file; NPlayer automatically injects its styles into the
<head>at runtime.import Player from 'nplayer' const player = new Player() player.mount('#app')Run website in local development mode
mainTo start a local development server, runyarn start. This will open a browser window and support live reloading for most changes.yarn startDeploy the website to GitHub Pages
mainTo build the website and push it to the
gh-pagesbranch for hosting on GitHub Pages, use theyarn deploycommand. You must provide your GitHub username via theGIT_USERenvironment variable. If you use SSH for Git operations, setUSE_SSH=true.GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy