inleft
2022-08-25 c6793e5475b607e83cbb55b7d0ddfb9b81bd7774
commit | author | age
105729 1 <template>
I 2     <div>
c6793e 3         <div v-if="!this.$attrs.showTar">
e4f086 4             正在播放: <span> {{playingVideoData.title}} </span>
0dd41b 5         </div>
1cf6d9 6         <div class="myVideo fade">
0dd41b 7             <div id="videoPlay" ref="myVdeoPlay"></div>
I 8         </div>
c6793e 9         <div style="border-radius: 0 0 4px 4px;" v-if="!this.$attrs.showTar">
8ec9c5 10
0dd41b 11             <div style="padding: 10px 0px;">
I 12                 音量 :
13                 <a-slider v-model="volumeInit" :default-value="0.1" :step="0.1" :max="1" @change="changeVolume" />
1cf6d9 14             </div>
I 15             <div style="padding: 10px 0px;">
16                 播放进度 : {{Math.floor(currentTime/60)}}:{{Math.floor(currentTime%60)}} /
17                 {{Math.floor(duration/60)}}:{{Math.floor(duration%60)}}
18                 <a-slider v-model="currentTime" :max="duration" @afterChange="changeTime" />
105729 19             </div>
8ec9c5 20
e4f086 21             <a-collapse activeKey="playHistory">
I 22                 <a-collapse-panel key="playHistory" header="播放记录">
23                     <div v-for="(item,index) in playHistory">
24                         <div v-if="index==0">
25                             <a-tooltip placement="bottomRight" title="播放/暂停">
26                                 <div @click="togglePlay" class="playItem">
27                                     {{item.title}}
1cf6d9 28                                     <span v-if="!isPlaying " class="myTip">(已暂停)</span>
I 29                                     <a-icon type="youtube" class="playIcon "
30                                         v-bind:class="{ activeElement: isPlaying }" />
e4f086 31                                 </div>
I 32                             </a-tooltip>
33                         </div>
34                         <div v-else>
35                             <div @click="swichPlay(item.id)" class="playItem">
36                                 <a-tooltip placement="bottomRight" title="切换播放">
37                                     <div>
38                                         {{item.title}}
39                                     </div>
40                                 </a-tooltip>
41                             </div>
42                         </div>
0dd41b 43                     </div>
I 44                 </a-collapse-panel>
45             </a-collapse>
105729 46         </div>
I 47     </div>
48 </template>
e4f086 49
105729 50
I 51 <script>
52     import Hls from 'hls.js'
53     import DPlayer from 'dplayer'
54
55     export default {
56         data() {
57             return {
58                 dp: null,
e4f086 59                 playHistory: [],
I 60                 playRecordIds: [],
0dd41b 61                 volumeInit: 0.1,
I 62                 isPlaying: false,
e4f086 63                 swichLock: false,
1cf6d9 64                 currentTime: 0,
I 65                 duration: 0,
e4f086 66                 playingVideoData: {
I 67                     img: "http://t.inleft.com/share/media_photo/idea_beijing.jpg",
68                     url: "",
69                     type: "normal",
70                     title: "",
71                     id: ""
72                 },
1cf6d9 73                 hls: null,
105729 74             }
I 75         },
0dd41b 76         mounted() {
I 77             //弹窗初始化后先不加载视频,等待手动播放
78             this.init();
e4f086 79             var _this = this;
I 80
81             //调整音量
a3ab3a 82             _this.$message.success("模块加载完成,请注意音量是否合适..")
e4f086 83
I 84             this.dp.volume(this.volumeInit, true, false);
85
86             this.dp.on('pause', function() {
87                 _this.$message.info("停止播放")
1cf6d9 88                 _this.isPlaying = false;
e4f086 89             });
I 90
91             this.dp.on('play', function() {
92                 _this.$message.info("开始播放..")
1cf6d9 93                 _this.isPlaying = true;
I 94                 _this.duration = _this.dp.video.duration;
95                 _this.currentTime = _this.dp.video.currentTime;
e4f086 96             });
1cf6d9 97             this.dp.on('timeupdate', function() {
I 98                 //_this.$message.info("进度..")
99                 _this.currentTime = _this.dp.video.currentTime;
100             });
101             this.dp.on('volumechange', function() {
102                 _this.volumeInit = _this.dp.video.volume;
103             });
104
0dd41b 105         },
105729 106         methods: {
8ec9c5 107             pauseMyVideo() {
I 108                 this.dp.pause();
109                 this.isPlaying = false;
110             },
e4f086 111             togglePlay() {
I 112                 this.dp.toggle();
113                 this.isPlaying = !this.isPlaying;
114             },
0dd41b 115             changeVolume(value) {
I 116                 this.dp.volume(value, true, false);
1cf6d9 117             },
I 118             changeTime(time) {
119                 this.dp.seek(time)
af029b 120             },
e4f086 121             //切换视频源
I 122             swichPlay(videoId) {
123                 if (this.swichLock) {
124                     this.$message.info("切换太快啦,缓一缓..", 5)
0dd41b 125                     return
I 126                 }
127
e4f086 128                 if (videoId == this.playingVideoData.id) {
I 129                     this.$message.info("同一个视频..", 1)
130                     return
131                 }
132                 let tempData = this.playHistory.filter(item => item.id == videoId)[0]
af029b 133
e4f086 134                 this.$message.info("切换视频源..", 1)
I 135                 this.swichLock = !this.swichLock;
af029b 136
1cf6d9 137
e4f086 138                 this.startPlay(tempData)
0dd41b 139
e4f086 140                 setTimeout(() => {
I 141                     this.swichLock = !this.swichLock;
1cf6d9 142                 }, 2000);
0dd41b 143
e4f086 144                 //通知父组件更换评论信息
I 145                 this.$emit("swichPlay", tempData)
0dd41b 146
I 147             },
e4f086 148             //调整播放记录
I 149             adjustOrder(videoData) {
150                 if (this.playRecordIds.findIndex(id => id == videoData.id) == -1) {
151                     //新纪录
152                     this.playRecordIds.push(videoData.id)
153                     this.playHistory.unshift(videoData)
154                 } else {
155                     //旧记录
156                     this.playHistory = this.playHistory.filter(item => item.id != videoData.id);
157                     this.playHistory.unshift(videoData)
158                 }
159             },
160             //加载播放
161             startPlay(videoData) {
a3ab3a 162                 if (videoData == null || videoData == undefined) {
I 163                     this.$message.error("无效资源..")
164                     return
165                 }
c6793e 166                 
I 167                 if (videoData.url != null && videoData.urlL != "" && videoData.url.endsWith("m3u8")) {
168                     videoData.type = "customHls";
169                 } else {
170                     videoData.type = "normal";
171                 }
a3ab3a 172
c6793e 173                 
1cf6d9 174                 //重要!!防止hls类型视频在切换后不停在后台缓存
I 175                 if (this.hls != null) {
176                     this.hls.destroy();
177                 }
e4f086 178
I 179                 this.adjustOrder(videoData)
180
181                 this.playingVideoData = videoData
182
183                 this.isPlaying = false;
184
1cf6d9 185                 //重要!!防止hls类型视频在切换后不停在后台缓存
I 186                 if (videoData.type == "customHls") {
187                     this.hls = new Hls();
188                 }
e4f086 189
a3ab3a 190                 this.dp.switchVideo(this.playingVideoData)
8ec9c5 191
e4f086 192             },
I 193             //播放器初始化
0dd41b 194             init() {
1cf6d9 195                 var _this = this;
105729 196                 this.dp = new DPlayer({
I 197                     element: document.getElementById("videoPlay"),
198                     //logo: "https://qczh-1252727916.cos.ap-nanjing.myqcloud.com/pic/273658f508d04d488414fd2b84c9f923.png", // 在视频左上角上打一个logo
0dd41b 199                     hotkey: true, // 是否支持热键,调节音量,播放,暂停等
105729 200                     mutex: false, //  防止同时播放多个用户,在该用户开始播放时暂停其他用户
I 201                     theme: "#b7daff", // 风格颜色,例如播放条,音量条的颜色
202                     loop: false, // 是否自动循环
203                     lang: "zh-cn", // 语言,'en', 'zh-cn', 'zh-tw'
204                     // screenshot: true, // 是否允许截图(按钮),点击可以自动将截图下载到本地
0dd41b 205                     preload: "metadata", // 自动预加载 'none', 'metadata', 'auto'
1cf6d9 206                     volume: _this.volumeInit, // 初始化音量
105729 207                     playbackSpeed: [2, 1, 0.5], //可选的播放速度,可自定义
I 208                     contextmenu: [
209                         //右键菜单
210                         {
211                             text: "b站",
212                             link: "https://www.bilibili.com"
213                         },
0dd41b 214
105729 215                     ],
I 216                     highlight: [
217                         //进度条上的自定义时间标记
218                         // 进度条时间点高亮
0dd41b 219                         // {
I 220                         //     text: "10M",
221                         //     time: 60
222                         // },
105729 223                     ],
I 224                     video: {
1cf6d9 225                         pic: _this.defalutImg, // 视频封面
0dd41b 226                         // url: videoInfo.url,
e4f086 227                         //url: "http://t.inleft.com/share/mp3/EOPMusic%282%29.mp3",
I 228                         type: "normal",
105729 229                         customType: {
I 230                             customHls: function(video, player) {
1cf6d9 231                                 //const hls = new Hls()
I 232                                 if (_this.hls != null) {
233                                     _this.hls.loadSource(video.src)
234                                     _this.hls.attachMedia(video)
235                                 }
105729 236                             }
I 237                         }
238                     }
239                 })
e4f086 240
I 241                 // 以下为隐藏一些作者的信息和视频播放源 如不需要可删除
242                 // document.querySelector(".dplayer-menu").remove(); //隐藏右键菜单
243
244                 document.querySelector(".dplayer-mask").remove();
245                 document.querySelector(".dplayer-info-panel-item-url").remove(); //隐藏播放源
246                 let length = document.querySelectorAll(".dplayer-menu-item").length;
247                 document.querySelectorAll(".dplayer-menu-item")[length - 1].remove(); // 去掉作者信息
248                 document.querySelectorAll(".dplayer-menu-item")[length - 2].remove(); // 去掉作者信息
0dd41b 249             },
105729 250         }
af029b 251
105729 252     }
I 253 </script>
254 <style lang="less" scoped>
0dd41b 255     .playItem {
I 256         border: 1px solid #d9d9d9;
257         padding: 10px;
258         border-radius: 5px;
259         margin: 10px;
1cf6d9 260         transition: all .3s;
0dd41b 261         position: relative;
I 262     }
263
8ec9c5 264     .mark {
I 265         border-radius: 14px;
266         background: #00000073;
267         width: 100%;
268         height: 100%;
269         pointer-events: none;
270     }
271
0dd41b 272     .playItem:hover {
e4f086 273         //transform: scale(1.1) translate3d(0, 0, 0);
0dd41b 274     }
I 275
276     .playItem:active {
1cf6d9 277         // transform: scale(0.75) translate3d(0, 0, 0);
I 278         box-shadow: 8px 8px 18px rgba(0, 0, 0, 0.1), -8px -8px 18px #ffffff;
0dd41b 279     }
e4f086 280
0dd41b 281
I 282     .playIcon {
283         transform: scale(1.3);
284         position: absolute;
285         top: 35%;
286         right: 10px;
287     }
288
105729 289     .myVideo {
I 290         position: relative;
1cf6d9 291         width: 315px;
105729 292         height: 180px;
I 293         background-color: #565656;
294         border-radius: 10px;
0dd41b 295         margin: 20px 0px;
e4f086 296         display: grid;
105729 297     }
I 298
299
0dd41b 300     .myVideo /deep/ .dplayer {
I 301         border-radius: 5px;
105729 302     }
I 303
1cf6d9 304     .activeElement {
I 305         -webkit-animation: free_download 1s linear alternate infinite;
306         animation: free_download 1s linear alternate infinite;
307     }
308
309     @-webkit-keyframes free_download {
310         0% {
311             -webkit-transform: scale(1);
312         }
313
314         100% {
315             -webkit-transform: scale(1.2);
316         }
317     }
318
319     @keyframes free_download {
320         0% {
321             transform: scale(1);
322         }
323
324         100% {
325             transform: scale(1.2);
326         }
327     }
105729 328 </style>