index.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. <el-table-column prop="filename" label="文件名"></el-table-column>
  44. <el-table-column label="权重" width="150">
  45. <template slot-scope="{row}">
  46. <el-input-number v-model="row.weight" :min="0" :max="10" @change="updateWeight(row)"
  47. size="mini">
  48. </el-input-number>
  49. </template>
  50. </el-table-column>
  51. </el-table>
  52. </el-aside>
  53. </el-container>
  54. </div>
  55. <script src="https://web.tianyunperfect.cn/simple/js/vue.min.js"></script>
  56. <script src="https://web.tianyunperfect.cn/simple/elementUI/index.js"></script>
  57. <script src="https://web.tianyunperfect.cn/simple/js/axios.min.js"></script>
  58. <script>
  59. new Vue({
  60. el: '#app',
  61. data() {
  62. return {
  63. files: [],
  64. currentVideo: '',
  65. currentVideoMD5: '',
  66. loopMode: true,
  67. infiniteLoop: false
  68. ,
  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. })
  167. } catch (error) {
  168. this.$message.error('获取视频失败')
  169. }
  170. },
  171. // 修改后的播放当前视频方法
  172. playCurrent() {
  173. const player = this.$refs.videoPlayer;
  174. // 直接操作播放器状态而不是修改src
  175. player.currentTime = 0;
  176. player.play().catch(error => {
  177. this.$message.warning('需要手动点击播放(浏览器限制)');
  178. });
  179. },
  180. handleVideoEnd() {
  181. if (this.infiniteLoop) {
  182. this.playCurrent()
  183. } else if (this.loopMode) {
  184. this.getRandomVideo(false)
  185. }
  186. },
  187. toggleInfiniteLoop() {
  188. if (this.infiniteLoop && !this.currentVideoMD5) {
  189. this.$message.warning('请先选择要循环的视频')
  190. this.infiniteLoop = false
  191. }
  192. }
  193. }
  194. })
  195. </script>
  196. </body>
  197. </html>