From d1523e749de2b64c4172b5bfd337691ec886057c Mon Sep 17 00:00:00 2001 From: Aicirou <2643053021@qq.com> Date: Sat, 4 Jul 2020 21:19:31 +0800 Subject: [PATCH] :art:Optimize video player --- public/setting.js | 78 +++++++++---------- src/plugin/vplayer/flv.js | 34 +++++++++ src/plugin/vplayer/hls.js | 32 ++++++++ src/plugin/vplayer/index.js | 4 + src/views/page/GoVideo.vue | 148 ++++++++++++++++-------------------- 5 files changed, 175 insertions(+), 121 deletions(-) create mode 100644 src/plugin/vplayer/flv.js create mode 100644 src/plugin/vplayer/hls.js create mode 100644 src/plugin/vplayer/index.js diff --git a/public/setting.js b/public/setting.js index f393a64..cbc8032 100644 --- a/public/setting.js +++ b/public/setting.js @@ -1,41 +1,41 @@ var authConfig = { - version: '1.1.1', - roots: [ - { - id: "", - name: "TeamDrive", - pass: "", - }, - { - id: "root", - name: "PriveDrive", - pass: "", - }, - { - id: "", - name: "folder1", - pass: "", - } - ], - }; - var themeOptions = { - // en/zh-chs/zh-cht - languages: 'en', - render: { - head_md: true, - readme_md: true, - // 是否显示文件/文件夹描述(默认不显示) - // Show file/folder description or not (not shown by default) - desc: true, + version: "1.1.1", + roots: [ + { + id: "", + name: "TeamDrive", + pass: "", }, - player: { - api: "", - autoplay: true - } - } - window.gdconfig = JSON.parse(JSON.stringify({ version: authConfig.version, themeOptions: themeOptions })); - window.themeOptions = themeOptions; - window.gds = JSON.parse( - JSON.stringify(authConfig.roots.map((it) => it.name)) - ); - window.current_drive_order = 0; \ No newline at end of file + { + id: "root", + name: "PriveDrive", + pass: "", + }, + { + id: "", + name: "folder1", + pass: "", + }, + ], +}; +var themeOptions = { + // en/zh-chs/zh-cht + languages: "en", + render: { + head_md: true, + readme_md: true, + // 是否显示文件/文件夹描述(默认不显示) + // Show file/folder description or not (not shown by default) + desc: true, + }, + video: { + api: "", + autoplay: true, + }, +}; +window.gdconfig = JSON.parse( + JSON.stringify({ version: authConfig.version, themeOptions: themeOptions }) +); +window.themeOptions = themeOptions; +window.gds = JSON.parse(JSON.stringify(authConfig.roots.map((it) => it.name))); +window.current_drive_order = 0; diff --git a/src/plugin/vplayer/flv.js b/src/plugin/vplayer/flv.js new file mode 100644 index 0000000..3e87287 --- /dev/null +++ b/src/plugin/vplayer/flv.js @@ -0,0 +1,34 @@ +import flvjs from "flv.js"; + +function FlvPlayer({ + src, + autoplay = false, + media, + options = { + type: "flv", + url: src, + }, + callback = () => ({}), +}) { + if (flvjs.isSupported()) { + var player = flvjs.createPlayer(options); + player.attachMediaElement(media); + player.load(); + player.on(flvjs.Events.ERROR, (event) => { + switch (event) { + case flvjs.ErrorTypes.NETWORK_ERROR: + player.load(); + break; + default: + player.destroy(); + break; + } + }); + if (autoplay) { + player.play(); + } + callback(player); + } +} + +export default FlvPlayer; diff --git a/src/plugin/vplayer/hls.js b/src/plugin/vplayer/hls.js new file mode 100644 index 0000000..8ac3e40 --- /dev/null +++ b/src/plugin/vplayer/hls.js @@ -0,0 +1,32 @@ +import Hls from "hls.js"; + +function HlsPlayer({ src, autoplay = false, media, callback = () => ({}) }) { + // eslint-disable-next-line no-undef + if (Hls.isSupported()) { + // For more Hls.js options, see https://github.com/dailymotion/hls.js + // eslint-disable-next-line no-undef + const hls = new Hls(); + hls.loadSource(src); + hls.attachMedia(media); + hls.on(Hls.Events.ERROR, function(event, data) { + if (data.fatal) { + switch (data.type) { + case Hls.ErrorTypes.NETWORK_ERROR: + hls.startLoad(); + break; + default: + hls.destroy(); + break; + } + } + }); + if (autoplay) { + hls.on(Hls.Events.MANIFEST_PARSED, function() { + media.play(); + }); + } + callback(hls); + } +} + +export default HlsPlayer; diff --git a/src/plugin/vplayer/index.js b/src/plugin/vplayer/index.js new file mode 100644 index 0000000..c5721a7 --- /dev/null +++ b/src/plugin/vplayer/index.js @@ -0,0 +1,4 @@ +import Flv from "./flv"; +import Hls from "./hls"; + +export { Flv, Hls }; diff --git a/src/views/page/GoVideo.vue b/src/views/page/GoVideo.vue index 2234c1a..070c33a 100644 --- a/src/views/page/GoVideo.vue +++ b/src/views/page/GoVideo.vue @@ -1,10 +1,10 @@