inleft
2022-08-24 80476fae71f08bf4408c7509eff254031a4eeac6
commit | author | age
105729 1 <template>
I 2     <div>
0dd41b 3         <div>
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>
9         <div style="border-radius: 0 0 4px 4px;">
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                 }
166
1cf6d9 167                 //重要!!防止hls类型视频在切换后不停在后台缓存
I 168                 if (this.hls != null) {
169                     this.hls.destroy();
170                 }
e4f086 171
I 172                 this.adjustOrder(videoData)
173
174                 this.playingVideoData = videoData
175
176                 this.isPlaying = false;
177
1cf6d9 178                 //重要!!防止hls类型视频在切换后不停在后台缓存
I 179                 if (videoData.type == "customHls") {
180                     this.hls = new Hls();
181                 }
e4f086 182
a3ab3a 183                 this.dp.switchVideo(this.playingVideoData)
8ec9c5 184
e4f086 185             },
I 186             //播放器初始化
0dd41b 187             init() {
1cf6d9 188                 var _this = this;
105729 189                 this.dp = new DPlayer({
I 190                     element: document.getElementById("videoPlay"),
191                     //logo: "https://qczh-1252727916.cos.ap-nanjing.myqcloud.com/pic/273658f508d04d488414fd2b84c9f923.png", // 在视频左上角上打一个logo
0dd41b 192                     hotkey: true, // 是否支持热键,调节音量,播放,暂停等
105729 193                     mutex: false, //  防止同时播放多个用户,在该用户开始播放时暂停其他用户
I 194                     theme: "#b7daff", // 风格颜色,例如播放条,音量条的颜色
195                     loop: false, // 是否自动循环
196                     lang: "zh-cn", // 语言,'en', 'zh-cn', 'zh-tw'
197                     // screenshot: true, // 是否允许截图(按钮),点击可以自动将截图下载到本地
0dd41b 198                     preload: "metadata", // 自动预加载 'none', 'metadata', 'auto'
1cf6d9 199                     volume: _this.volumeInit, // 初始化音量
105729 200                     playbackSpeed: [2, 1, 0.5], //可选的播放速度,可自定义
I 201                     contextmenu: [
202                         //右键菜单
203                         {
204                             text: "b站",
205                             link: "https://www.bilibili.com"
206                         },
0dd41b 207
105729 208                     ],
I 209                     highlight: [
210                         //进度条上的自定义时间标记
211                         // 进度条时间点高亮
0dd41b 212                         // {
I 213                         //     text: "10M",
214                         //     time: 60
215                         // },
105729 216                     ],
I 217                     video: {
1cf6d9 218                         pic: _this.defalutImg, // 视频封面
0dd41b 219                         // url: videoInfo.url,
e4f086 220                         //url: "http://t.inleft.com/share/mp3/EOPMusic%282%29.mp3",
I 221                         type: "normal",
105729 222                         customType: {
I 223                             customHls: function(video, player) {
1cf6d9 224                                 //const hls = new Hls()
I 225                                 if (_this.hls != null) {
226                                     _this.hls.loadSource(video.src)
227                                     _this.hls.attachMedia(video)
228                                 }
105729 229                             }
I 230                         }
231                     }
232                 })
e4f086 233
I 234                 // 以下为隐藏一些作者的信息和视频播放源 如不需要可删除
235                 // document.querySelector(".dplayer-menu").remove(); //隐藏右键菜单
236
237                 document.querySelector(".dplayer-mask").remove();
238                 document.querySelector(".dplayer-info-panel-item-url").remove(); //隐藏播放源
239                 let length = document.querySelectorAll(".dplayer-menu-item").length;
240                 document.querySelectorAll(".dplayer-menu-item")[length - 1].remove(); // 去掉作者信息
241                 document.querySelectorAll(".dplayer-menu-item")[length - 2].remove(); // 去掉作者信息
0dd41b 242             },
105729 243         }
af029b 244
105729 245     }
I 246 </script>
247 <style lang="less" scoped>
0dd41b 248     .playItem {
I 249         border: 1px solid #d9d9d9;
250         padding: 10px;
251         border-radius: 5px;
252         margin: 10px;
1cf6d9 253         transition: all .3s;
0dd41b 254         position: relative;
I 255     }
256
8ec9c5 257     .mark {
I 258         border-radius: 14px;
259         background: #00000073;
260         width: 100%;
261         height: 100%;
262         pointer-events: none;
263     }
264
0dd41b 265     .playItem:hover {
e4f086 266         //transform: scale(1.1) translate3d(0, 0, 0);
0dd41b 267     }
I 268
269     .playItem:active {
1cf6d9 270         // transform: scale(0.75) translate3d(0, 0, 0);
I 271         box-shadow: 8px 8px 18px rgba(0, 0, 0, 0.1), -8px -8px 18px #ffffff;
0dd41b 272     }
e4f086 273
0dd41b 274
I 275     .playIcon {
276         transform: scale(1.3);
277         position: absolute;
278         top: 35%;
279         right: 10px;
280     }
281
105729 282     .myVideo {
I 283         position: relative;
1cf6d9 284         width: 315px;
105729 285         height: 180px;
I 286         background-color: #565656;
287         border-radius: 10px;
0dd41b 288         margin: 20px 0px;
e4f086 289         display: grid;
105729 290     }
I 291
292
0dd41b 293     .myVideo /deep/ .dplayer {
I 294         border-radius: 5px;
105729 295     }
I 296
1cf6d9 297     .activeElement {
I 298         -webkit-animation: free_download 1s linear alternate infinite;
299         animation: free_download 1s linear alternate infinite;
300     }
301
302     @-webkit-keyframes free_download {
303         0% {
304             -webkit-transform: scale(1);
305         }
306
307         100% {
308             -webkit-transform: scale(1.2);
309         }
310     }
311
312     @keyframes free_download {
313         0% {
314             transform: scale(1);
315         }
316
317         100% {
318             transform: scale(1.2);
319         }
320     }
105729 321 </style>