JessVideoPlayer.vue
7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<template>
<div class="root">
<div id="container" ref="container" class="player-container">
<!-- 重试按钮 -->
<div v-if="showRetryButton" class="retry-overlay" @click="handleRetry">
<el-icon class="retry-icon"><refresh /></el-icon>
</div>
</div>
</div>
</template>
<script>
import userService from '../service/UserService';
export default {
name: "VideoPlayer",
props: {
initialBufferTime: {
type: Number,
default: 0.2
},
initialPlayUrl: {
type: String,
default: ''
}
},
data() {
return {
jessibuca: null,
version: '',
wasm: false,
vc: "ff",
playing: false,
quieting: true,
loaded: false, // mute
showOperateBtns: true,
showBandwidth: true,
err: "",
speed: 0,
performance: "未知",
volume: 1,
rotate: 0,
useWCS: false,
useMSE: true,
useOffscreen: false,
recording: false,
recordType: 'mp4',
scale: 0.2,
bufferTime: this.initialBufferTime,
playUrl: this.initialPlayUrl,
showRetryButton: false, // 重试按钮
timeoutTimer: null, // 重试定时器
config: {
userName: userService.getUser().username,
nowTime: "2024-03-01 12:00:00"
} // 水印
};
},
watch: {
/**
* 监听initialPlayUrl变化
* @param val 播放地址
*/
initialPlayUrl(val) {
this.destroy()
console.log('initialPlayUrl changed:', val);
this.playUrl = val;
if (this.jessibuca && this.playUrl){
this.play();
}
}
},
beforeDestroy() {
}, //生命周期 - 销毁之前",
destroyed() {
this.destroy()
}, //生命周期 - 销毁完成",
// 在播放成功时清除定时器
mounted() {
this.create();
this.$errorHandler = this.handleError;
this.jessibuca.on('play', () => {
clearTimeout(this.timeoutTimer);
});
if (this.playUrl) {
this.play();
}
},
unmounted() {
if (this.jessibuca) {
this.jessibuca.destroy();
}
},
methods: {
updatePlayerDomSize() {
let dom = this.$refs.container;
let width = dom.parentNode.clientWidth
let height = (9 / 16) * width
console.log(height)
console.log(dom.clientHeight)
if (height > dom.clientHeight) {
height = dom.clientHeight
width = (16 / 9) * height
}
if (width > 0 && height > 0) {
dom.style.width = width + 'px';
dom.style.height = height + "px";
console.log(width)
console.log(height)
}
},
create(options) {
// 初始化Jessibuca播放器
options = options || {};
this.jessibuca = new window.Jessibuca(
Object.assign(
{
container: this.$refs.container,
background: "#000", //背景图片
controlAutoHide: true, //底部控制台自动隐藏
videoBuffer: Number(this.bufferTime), // 缓存时长
isResize: false,
autoWasm: true,
useWCS: this.useWCS,
useMSE: this.useMSE,
decoder: "static/js/jessibuca/decoder.js",
text: "",
loadingText: "疯狂加载中...",
debug: false,
supportDblclickFullscreen: true,
showBandwidth: this.showBandwidth, // 显示网速
operateBtns: {
fullscreen: this.showOperateBtns,
screenshot: this.showOperateBtns,
audio: this.showOperateBtns,
record: this.showOperateBtns,
refresh: this.showOperateBtns,
},
vod: this.vod,
forceNoOffscreen: !this.useOffscreen,
isNotMute: true,
timeout: 10
},
options
)
);
// 处理播放器事件
const handleEvent = (event, callback) => {
this.jessibuca.on(event, (...args) => {
console.log(`on ${event}`, ...args);
callback && callback(...args);
});
};
handleEvent("load", () => {
});
handleEvent("log", msg => {
console.log(`on log`, msg)
});
handleEvent("record", msg => {
console.log(`on record`, msg)
});
handleEvent("pause", () => this.playing = false);
handleEvent("play", () => {
this.playing = true;
this.loaded = true;
this.quieting = this.jessibuca.isMute();
});
handleEvent("fullscreen", msg => {
console.log(`on fullscreen`, msg)
});
handleEvent("mute", msg => this.quieting = msg);
handleEvent("audioInfo", msg => {
console.log(`on audioInfo`, msg)
});
handleEvent("videoInfo", info => {
console.log(`on videoInfo`, info)
});
handleEvent("error", error => this.handleError(error));
handleEvent("timeout", () => {
});
handleEvent("start", () => {
});
handleEvent("performance", performance => {
this.performance = ["卡顿", "流畅", "非常流畅"][performance] || "未知";
});
handleEvent("buffer", buffer => {
console.log(`on buffer`, buffer)
});
handleEvent("stats", stats => {
console.log(`on stats`, stats)
});
handleEvent("kBps", kBps => {
console.log(`on kBps`, kBps)
});
handleEvent("recordingTimestamp", ts => {
console.log(`on recordingTimestamp`, ts)
});
},
handleError(msg) {
// 处理错误信息
this.err = msg;
// 触发自定义事件,通知父组件播放失败
this.$emit('play-error', this.playUrl);
},
play() {
// 重置状态
this.showRetryButton = false;
clearTimeout(this.timeoutTimer);
if (!this.playUrl) {
console.log("playUrl 无效 ===>", this.playUrl);
return;
}
// 设置超时检测
this.timeoutTimer = setTimeout(() => {
if (!this.playing) {
this.showRetryButton = true;
}
}, 10000); // 10秒超时
this.jessibuca.play(this.playUrl);
},
handleRetry() {
this.showRetryButton = false;
this.destroy();
this.$nextTick(() => {
this.create();
this.play();
});
},
pause() {
// 暂停视频
this.jessibuca.pause();
this.playing = false;
this.performance = "未知";
},
destroy() {
// 销毁并重新创建播放器
if (this.jessibuca) {
this.jessibuca.destroy();
}
this.create();
this.playing = false;
this.loaded = false;
this.performance = "未知";
},
startRecord() {
// 开始录制视频
const time = new Date().getTime();
this.jessibuca.startRecord(time, this.recordType);
},
stopAndSaveRecord() {
// 停止并保存录制的视频
this.jessibuca.stopRecordAndSave();
},
changeBuffer() {
// 改变缓冲时间
this.jessibuca.setBufferTime(Number(this.bufferTime));
},
},
};
</script>
<style scoped>
.player-container {
position: relative;
width: 100%;
height: 100%;
}
.retry-overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
background: rgba(0, 0, 0, 0.5);
border-radius: 50%;
padding: 20px;
transition: all 0.3s;
}
.retry-overlay:hover {
background: rgba(0, 0, 0, 0.7);
}
.retry-icon {
font-size: 40px;
color: rgba(255, 255, 255, 0.8);
display: block;
}
.retry-icon:hover {
color: rgba(255, 255, 255, 1);
}
</style>