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