index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>视频随机播放器</title>
  6. <link rel="stylesheet" href="https://web.tianyunperfect.cn/simple/elementUI/index.css">
  7. </head>
  8. <body>
  9. <div id="app">
  10. <el-container style="height: 100vh;">
  11. <!-- 视频播放区域 -->
  12. <el-main>
  13. <div style="margin-bottom: 20px; display: flex; align-items: center; gap: 20px; flex-wrap: nowrap;">
  14. <el-button @click="getRandomVideo" type="primary">随机播放</el-button>
  15. <el-radio-group v-model="selectedFileType" size="mini" style="margin-left: 10px;">
  16. <el-radio-button label="video">视频</el-radio-button>
  17. <el-radio-button label="audio">音频</el-radio-button>
  18. <el-radio-button label="all">全部</el-radio-button>
  19. </el-radio-group>
  20. <el-switch v-model="loopMode" active-text="连播" active-color="#13ce66" inactive-color="#ff4949"
  21. style="margin-left: 15px;"></el-switch>
  22. <el-switch v-model="infiniteLoop" active-text="循环播放" @change="toggleInfiniteLoop"
  23. style="flex-shrink: 0;">
  24. </el-switch>
  25. <div v-if="currentFile" style="display: flex; align-items: center; flex-shrink: 0;">
  26. <span style="margin-right: 10px;">当前播放:</span>
  27. <div style="font-weight: bold;">{{ currentFile.filename }}</div>
  28. <span style="margin-left: 15px; margin-right: 10px;">权重:</span>
  29. <el-input-number v-model="currentFile.weight" :min="0" :max="10" size="mini"
  30. @change="updateWeight(currentFile)" style="width: 100px;">
  31. </el-input-number>
  32. </div>
  33. </div>
  34. <video ref="videoPlayer" controls :src="currentVideo" style="width: 100%; max-height: 80vh;" controls
  35. preload="none" @ended="handleVideoEnd">
  36. 您的浏览器不支持视频播放
  37. </video>
  38. </el-main>
  39. <!-- 视频列表 -->
  40. <el-aside width="450px" style="background: #f5f5f5; height: 100vh;">
  41. <h3 style="margin-bottom: 15px;">视频列表(双击播放)</h3>
  42. <el-table :data="filteredFiles" stripe height="calc(100vh - 60px)" @row-dblclick="playSelected"
  43. :row-class-name="tableRowClassName">
  44. <el-table-column prop="filename" label="文件名"></el-table-column>
  45. <el-table-column label="权重" width="150">
  46. <template slot-scope="{row}">
  47. <el-input-number v-model="row.weight" :min="0" :max="10" @change="updateWeight(row)"
  48. size="mini">
  49. </el-input-number>
  50. </template>
  51. </el-table-column>
  52. </el-table>
  53. </el-aside>
  54. </el-container>
  55. </div>
  56. <script src="https://web.tianyunperfect.cn/simple/js/vue.min.js"></script>
  57. <script src="https://web.tianyunperfect.cn/simple/elementUI/index.js"></script>
  58. <script src="https://web.tianyunperfect.cn/simple/js/axios.min.js"></script>
  59. <script>
  60. new Vue({
  61. el: '#app',
  62. data() {
  63. return {
  64. files: [],
  65. currentVideo: '',
  66. currentVideoMD5: '',
  67. loopMode: true,
  68. infiniteLoop: false,
  69. selectedFileType: 'video'
  70. }
  71. },
  72. mounted() {
  73. this.fetchFiles()
  74. // 处理页面可见性变化
  75. document.addEventListener('visibilitychange', () => {
  76. if (document.visibilityState === 'visible') {
  77. this.handlePageVisible();
  78. }
  79. });
  80. },
  81. computed: {
  82. currentFile() {
  83. return this.files.find(file => file.md5 === this.currentVideoMD5)
  84. },
  85. filteredFiles() {
  86. const videoExts = ['mp4', 'avi', 'mov', 'mkv'];
  87. const audioExts = ['aac', 'mp3', 'flac'];
  88. return this.files.filter(file => {
  89. const ext = file.filename.split('.').pop().toLowerCase();
  90. if (this.selectedFileType === 'video') return videoExts.includes(ext);
  91. if (this.selectedFileType === 'audio') return audioExts.includes(ext);
  92. return true;
  93. });
  94. },
  95. },
  96. methods: {
  97. // 新增方法: 页面可见时处理
  98. handlePageVisible() {
  99. const player = this.$refs.videoPlayer;
  100. if (!player) return;
  101. // 记录当前播放状态
  102. const wasPlaying = !player.paused;
  103. player.pause();
  104. if (wasPlaying) {
  105. player.currentTime = player.currentTime - 0.001;
  106. player.play();
  107. }
  108. },
  109. // 加权随机选择方法
  110. getWeightedRandom(files) {
  111. // 过滤掉权重为0的文件,确保它们不被选中
  112. const validFiles = files.filter(file => (file.weight ?? 1) > 0);
  113. if (validFiles.length === 0) return null; // 处理所有文件权重为0的情况
  114. const totalWeight = validFiles.reduce((sum, file) => sum + (file.weight ?? 1), 0);
  115. const random = Math.random() * totalWeight;
  116. let currentSum = 0;
  117. for (const file of validFiles) {
  118. currentSum += file.weight ?? 1;
  119. if (random <= currentSum) return file;
  120. }
  121. return validFiles[validFiles.length - 1];
  122. }, async fetchFiles() {
  123. const res = await axios.get('/files')
  124. this.files = res.data
  125. },
  126. // 播放选中视频(新增)
  127. playSelected(row) {
  128. this.currentVideoMD5 = row.md5;
  129. this.currentVideo = `/play/${row.md5}?t=${Date.now()}`;
  130. this.$nextTick(() => {
  131. const player = this.$refs.videoPlayer;
  132. player.pause();
  133. player.load(); // 重新加载新源
  134. player.onloadeddata = () => {
  135. player.play().catch(error => {
  136. this.$message.warning('需要手动点击播放(浏览器限制)');
  137. });
  138. };
  139. });
  140. },
  141. // 权重更新(修改后)
  142. async updateWeight(row) {
  143. try {
  144. await axios.post(`/weight/${row.md5}`, {
  145. weight: row.weight
  146. })
  147. this.$message.success('权重保存成功')
  148. } catch (error) {
  149. this.$message.error('保存失败')
  150. console.error('保存权重失败:', error)
  151. }
  152. },
  153. async getRandomVideo(forceNew = true) {
  154. try {
  155. if (this.infiniteLoop && this.currentVideoMD5 && !forceNew) {
  156. return this.playCurrent()
  157. }
  158. const res = this.getWeightedRandom(this.filteredFiles);
  159. this.currentVideoMD5 = res.md5
  160. this.currentVideo = `/play/${res.md5}`
  161. this.$nextTick(() => {
  162. const player = this.$refs.videoPlayer
  163. player.play().catch(error => {
  164. this.$message.warning('需要手动点击播放(浏览器限制)')
  165. })
  166. // 根据md5找到列表中的当前视频,如果不可见就滚动到可见
  167. const index = this.filteredFiles.findIndex(file => file.md5 === this.currentVideoMD5);
  168. if (index !== -1) {
  169. this.$nextTick(() => {
  170. const tableEl = this.$el.querySelector('.el-table__body-wrapper');
  171. const rowEl = tableEl.querySelectorAll('.el-table__row')[index];
  172. if (rowEl) {
  173. const rowTop = rowEl.offsetTop;
  174. const rowBottom = rowTop + rowEl.offsetHeight;
  175. const viewportTop = tableEl.scrollTop;
  176. const viewportBottom = viewportTop + tableEl.clientHeight;
  177. if (rowTop < viewportTop || rowBottom > viewportBottom) {
  178. tableEl.scrollTop = rowTop - tableEl.clientHeight / 2 + rowEl.offsetHeight / 2;
  179. }
  180. }
  181. });
  182. }
  183. })
  184. } catch (error) {
  185. this.$message.error('获取视频失败')
  186. }
  187. },
  188. // 修改后的播放当前视频方法
  189. playCurrent() {
  190. const player = this.$refs.videoPlayer;
  191. // 直接操作播放器状态而不是修改src
  192. player.currentTime = 0;
  193. player.play().catch(error => {
  194. this.$message.warning('需要手动点击播放(浏览器限制)');
  195. });
  196. },
  197. handleVideoEnd() {
  198. if (this.infiniteLoop) {
  199. this.playCurrent()
  200. } else if (this.loopMode) {
  201. this.getRandomVideo(false)
  202. }
  203. },
  204. toggleInfiniteLoop() {
  205. if (this.infiniteLoop && !this.currentVideoMD5) {
  206. this.$message.warning('请先选择要循环的视频')
  207. this.infiniteLoop = false
  208. }
  209. },
  210. // 新增方法: 表格行样式
  211. tableRowClassName({ row }) {
  212. if (row.md5 === this.currentVideoMD5) {
  213. return 'current-playing-row';
  214. }
  215. return '';
  216. }
  217. }
  218. })
  219. </script>
  220. <style>
  221. /* 新增样式: 当前行样式 */
  222. .current-playing-row td {
  223. background-color: #b1c6ef !important;
  224. }
  225. </style>
  226. </body>
  227. </html>