🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Vue-Video-Player vue视频播放器组件 ## 安装 ~~~shell npm install vue-video-player --save 或 yarn add vue-video-player ~~~ ## 引入 ### 全局 ~~~ import Vue from 'vue' import VueVideoPlayer from 'vue-video-player' // require videojs style import 'video.js/dist/video-js.css' // import 'vue-video-player/src/custom-theme.css' Vue.use(VueVideoPlayer, /* { options: global default options, events: global videojs events } */) ~~~ ### 局部 ~~~ // require styles import 'video.js/dist/video-js.css' import { videoPlayer } from 'vue-video-player' export default { components: { videoPlayer } } ~~~ ### ssr挂载 ~~~ // If used in nuxt.js/ssr, you should keep it only in browser build environment if (process.browser) { const VueVideoPlayer = require('vue-video-player/dist/ssr') Vue.use(VueVideoPlayer) } ~~~ ## videojs 扩展 ~~~ import videojs from 'video.js' // videojs plugin const Plugin = videojs.getPlugin('plugin') class ExamplePlugin extends Plugin { // something... } videojs.registerPlugin('examplePlugin', ExamplePlugin) // videojs language videojs.addLanguage('es', { Pause: 'Pausa', // something... }) // more videojs api... // vue component... ~~~ ## 示例 ### SPA ~~~ <template> <video-player class="video-player-box" ref="videoPlayer" :options="playerOptions" :playsinline="true" customEventName="customstatechangedeventname" @play="onPlayerPlay($event)" @pause="onPlayerPause($event)" @ended="onPlayerEnded($event)" @waiting="onPlayerWaiting($event)" @playing="onPlayerPlaying($event)" @loadeddata="onPlayerLoadeddata($event)" @timeupdate="onPlayerTimeupdate($event)" @canplay="onPlayerCanplay($event)" @canplaythrough="onPlayerCanplaythrough($event)" @statechanged="playerStateChanged($event)" @ready="playerReadied"> </video-player> </template> <script> // Similarly, you can also introduce the plugin resource pack you want to use within the component // import 'some-videojs-plugin' export default { data() { return { playerOptions: { // videojs options muted: true, language: 'en', playbackRates: [0.7, 1.0, 1.5, 2.0], sources: [{ type: "video/mp4", src: "https://cdn.theguardian.tv/webM/2015/07/20/150716YesMen_synd_768k_vp8.webm" }], poster: "/static/images/author.jpg", } } }, mounted() { console.log('this is current player instance object', this.player) }, computed: { player() { return this.$refs.videoPlayer.player } }, methods: { // listen event onPlayerPlay(player) { // console.log('player play!', player) }, onPlayerPause(player) { // console.log('player pause!', player) }, // ...player event // or listen state event playerStateChanged(playerCurrentState) { // console.log('player current update state', playerCurrentState) }, // player is ready playerReadied(player) { console.log('the player is readied', player) // you can use it to do something... // player.[methods] } } } </script> ~~~ ### SSR ~~~html <!-- You can custom the "myVideoPlayer" name used to find the videojs instance in current component --> <template> <div class="video-player-box" @play="onPlayerPlay($event)" @pause="onPlayerPause($event)" @ready="playerReadied" @statechanged="playerStateChanged($event)" v-video-player:myVideoPlayer="playerOptions"> </div> </template> <script> export default { mounted() { console.log('this is current videojs instance object', this.myVideoPlayer) } // Omit the same parts as in the following component sample code // ... } </script> ~~~ ## videojs API * [video.js options](http://docs.videojs.com/tutorial-options.html) * [video.js docs](http://docs.videojs.com/) ## videojs 插件 * [videojs-resolution-switcher](https://github.com/kmoskwiak/videojs-resolution-switcher) * [videojs-contrib-hls](https://github.com/videojs/videojs-contrib-hls) * [videojs-youtube](https://github.com/videojs/videojs-youtube) * [videojs-vimeo](https://github.com/videojs/videojs-vimeo) * [videojs-hotkeys](https://github.com/ctd1500/videojs-hotkeys) * [videojs-flash](https://github.com/videojs/videojs-flash) * [videojs-contrib-ads](https://github.com/videojs/videojs-contrib-ads) * [more plugins...](https://github.com/search?o=desc&q=videojs+plugin&s=stars&type=Repositories&utf8=%E2%9C%93)