Skip to content

Commit 0b48b0a

Browse files
authored
Merge pull request #33 from ZyqGitHub1/dev
add direct video player
2 parents 3b94d37 + fcf3f63 commit 0b48b0a

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

src/components/HlsPlayer.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export default {
5151
} else {
5252
const hls = new Hls();
5353
this.hls = hls;
54+
hls.on(Hls.Events.ERROR, (event, data) => {
55+
this.$emit('hls-error', event, data);
56+
});
5457
hls.loadSource(this.source);
5558
hls.attachMedia(this.video);
5659
this.$once('hook:beforeDestroy', () => {

src/layouts/Home.vue

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,13 @@
132132
import titleBar from 'components/titlebar';
133133
import { mapState, mapMutations, mapGetters } from 'vuex';
134134
import util from 'util';
135+
import isAbsoluteUrl from 'is-absolute-url';
135136
import { parseString } from 'xml2js';
136137
138+
import { URL } from 'url';
139+
import path from 'path';
140+
import { stringify } from 'query-string';
141+
137142
const Store = require('electron-store');
138143
139144
const store = new Store();
@@ -232,8 +237,50 @@ export default {
232237
this.setCurrentVideo(video);
233238
this.$router.push('/video');
234239
},
240+
directVideo() {
241+
const { BrowserWindow, getCurrentWindow } = this.$q.electron.remote;
242+
const videoInfo = JSON.stringify({
243+
url: this.keyWord,
244+
});
245+
const encodeUrl = stringify({ video: videoInfo });
246+
const parentWindow = getCurrentWindow();
247+
const win = new BrowserWindow({
248+
width: 800,
249+
height: 600,
250+
useContentSize: true,
251+
webPreferences: {
252+
nodeIntegration: true,
253+
webSecurity: false,
254+
},
255+
parent: parentWindow,
256+
});
257+
win.removeMenu();
258+
win.loadURL(`${process.env.APP_URL}#/direct-video?${encodeUrl}`);
259+
},
235260
search() {
236-
this.$store.commit('setKeyWord', this.keyWord);
261+
if (isAbsoluteUrl(this.keyWord)) {
262+
try {
263+
const url = new URL(this.keyWord);
264+
const { pathname } = url;
265+
const extname = path.extname(pathname);
266+
if (extname === '.m3u8') {
267+
this.$q.dialog({
268+
title: '播放',
269+
message: '检测到搜索参数是hls流链接,是否播放',
270+
cancel: true,
271+
persistent: true,
272+
}).onOk(() => {
273+
this.directVideo();
274+
}).onCancel(() => {
275+
this.$store.commit('setKeyWord', this.keyWord);
276+
});
277+
}
278+
} catch (error) {
279+
console.error(error);
280+
}
281+
} else {
282+
this.$store.commit('setKeyWord', this.keyWord);
283+
}
237284
},
238285
},
239286
computed: {

src/pages/directVideo.vue

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<template>
2+
<q-page
3+
class="video-warp"
4+
padding
5+
>
6+
<hls-player
7+
:source="normalizeUrl(videoUrl)"
8+
:options="options"
9+
@hls-error="errorHandler"
10+
ref="player"
11+
></hls-player>
12+
</q-page>
13+
</template>
14+
15+
<script>
16+
import HlsPlayer from 'components/HlsPlayer';
17+
import normalizeUrl from 'normalize-url';
18+
19+
export default {
20+
name: 'MiniVideo',
21+
data() {
22+
return {
23+
options: {
24+
controls: [
25+
'play-large',
26+
'play',
27+
'progress',
28+
'current-time',
29+
'mute',
30+
'volume',
31+
'captions',
32+
'settings',
33+
'airplay',
34+
'fullscreen',
35+
],
36+
},
37+
videoUrl: '',
38+
};
39+
},
40+
components: {
41+
HlsPlayer,
42+
},
43+
created() {
44+
const videoInfo = JSON.parse(this.$route.query.video);
45+
this.videoUrl = videoInfo.url;
46+
document.querySelector('title').text = videoInfo.url;
47+
},
48+
methods: {
49+
normalizeUrl(url) {
50+
const pureUrl = url.replace(/(.*?)\$/, '').replace(/\$(.*)/, '');
51+
return normalizeUrl(pureUrl);
52+
},
53+
errorHandler(event, data) {
54+
if (data.details && data.details === 'manifestLoadError') {
55+
this.$q.dialog({
56+
title: '错误',
57+
message: '播放失败,点击关闭窗口',
58+
persistent: true,
59+
}).onOk(() => {
60+
const { getCurrentWindow } = this.$q.electron.remote;
61+
const window = getCurrentWindow();
62+
window.close();
63+
});
64+
}
65+
},
66+
},
67+
};
68+
</script>
69+
70+
<style>
71+
.video-warp {
72+
height: 100vh;
73+
}
74+
</style>

src/router/routes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const routes = [
1616
component: () => import('layouts/Mini'),
1717
children: [{ path: '', component: () => import('pages/MiniVideo') }],
1818
},
19+
{
20+
path: '/direct-video',
21+
component: () => import('layouts/Mini'),
22+
children: [{ path: '', component: () => import('pages/directVideo') }],
23+
},
1924
{
2025
path: '/config',
2126
component: () => import('layouts/Config'),

0 commit comments

Comments
 (0)