Vue Plyr Video and Audio Player
Vue-plyr is a component library that provides a seamless integration of the robust Plyr.io media player into Vue.js applications. It supports HTML5 video and audio, as well as embeds from YouTube and Vimeo, offering a consistent and customizable player experience. Currently at version 7.0.0, vue-plyr aligns with modern Vue 3 applications while also providing backward compatibility for Vue 2. Its release cadence typically follows updates to the underlying Plyr library and major Vue.js versions. A key differentiator is its straightforward API for embedding various media types and accessing the underlying Plyr instance for advanced control, simplifying the process compared to integrating Plyr directly. The library also includes specific support for Server-Side Rendering (SSR) environments like Nuxt.js, addressing common challenges in universal applications.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'use')
cause Attempting to use `Vue.use()` for global component registration in a Vue 3 application, where `Vue` is no longer a global constructor.fixFor Vue 3, use the application instance's `use` method: `createApp(App).use(VuePlyr).mount('#app')`. -
Player displays with incorrect styling, missing controls, or is unformatted.
cause The essential CSS stylesheet for `vue-plyr` has not been loaded into the application.fixAdd `import 'vue-plyr/dist/vue-plyr.css'` to your application's entry point (e.g., `main.js`, `main.ts`). -
ReferenceError: window is not defined (during SSR build or development)
cause The default `vue-plyr` module attempts to access browser-specific APIs (like `window` or `document`) during the server-side rendering phase.fixImport the SSR-optimized version of the component: `import VuePlyr from 'vue-plyr/dist/vue-plyr.ssr.js'` in your SSR-specific files or use dynamic imports for client-only components.
Warnings
- breaking Version 7.x primarily targets Vue 3.x. While Vue 2.x support is maintained, the global registration syntax has changed from `Vue.use()` to `createApp().use()` for Vue 3 applications.
- gotcha Forgetting to import the required CSS stylesheet (`vue-plyr/dist/vue-plyr.css`) will result in an unstyled or visually broken player, as the component only provides functionality.
- gotcha When developing Server-Side Rendered (SSR) applications (e.g., with Nuxt.js), the standard client-side build of `vue-plyr` may cause build errors due to direct browser API access. An SSR-optimized module is available.
- gotcha To interact with the underlying Plyr.js player instance (e.g., to call methods like `play()`, `pause()`), you must use a `ref` on the `vue-plyr` component and access its `player` property.
Install
-
npm install vue-plyr -
yarn add vue-plyr -
pnpm add vue-plyr
Imports
- VuePlyr
const VuePlyr = require('vue-plyr')import VuePlyr from 'vue-plyr'
- CSS
import 'vue-plyr/css'
import 'vue-plyr/dist/vue-plyr.css'
- VuePlyr SSR
import VuePlyr from 'vue-plyr'
import VuePlyr from 'vue-plyr/dist/vue-plyr.ssr.js'
Quickstart
import { createApp } from 'vue'
import App from './App.vue'
import VuePlyr from 'vue-plyr'
import 'vue-plyr/dist/vue-plyr.css'
const app = createApp(App)
app.use(VuePlyr, { plyr: {} })
app.mount('#app')
// In App.vue or another component:
// <template>
// <vue-plyr :options="{autoplay: false, muted: false}">
// <video controls crossorigin playsinline>
// <source src="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-576p.mp4" type="video/mp4" />
// </video>
// </vue-plyr>
// </template>
// <script>
// export default {
// name: 'App'
// }
// </script>