index.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. computed: {
  76. currentFile() {
  77. return this.files.find(file => file.md5 === this.currentVideoMD5)
  78. },
  79. filteredFiles() {
  80. const videoExts = ['mp4', 'avi', 'mov', 'mkv'];
  81. const audioExts = ['aac', 'mp3', 'flac'];
  82. return this.files.filter(file => {
  83. const ext = file.filename.split('.').pop().toLowerCase();
  84. if (this.selectedFileType === 'video') return videoExts.includes(ext);
  85. if (this.selectedFileType === 'audio') return audioExts.includes(ext);
  86. return true;
  87. });
  88. },
  89. },
  90. methods: {
  91. // 加权随机选择方法
  92. getWeightedRandom(files) {
  93. // 过滤掉权重为0的文件,确保它们不被选中
  94. const validFiles = files.filter(file => (file.weight ?? 1) > 0);
  95. if (validFiles.length === 0) return null; // 处理所有文件权重为0的情况
  96. const totalWeight = validFiles.reduce((sum, file) => sum + (file.weight ?? 1), 0);
  97. const random = Math.random() * totalWeight;
  98. let currentSum = 0;
  99. for (const file of validFiles) {
  100. currentSum += file.weight ?? 1;
  101. if (random <= currentSum) return file;
  102. }
  103. return validFiles[validFiles.length - 1];
  104. }, async fetchFiles() {
  105. const res = await axios.get('/files')
  106. this.files = res.data
  107. },
  108. // 播放选中视频(新增)
  109. playSelected(row) {
  110. this.currentVideoMD5 = row.md5;
  111. this.currentVideo = `/play/${row.md5}?t=${Date.now()}`;
  112. this.$nextTick(() => {
  113. const player = this.$refs.videoPlayer;
  114. player.pause();
  115. player.load(); // 重新加载新源
  116. player.onloadeddata = () => {
  117. player.play().catch(error => {
  118. this.$message.warning('需要手动点击播放(浏览器限制)');
  119. });
  120. };
  121. });
  122. },
  123. // 权重更新(修改后)
  124. async updateWeight(row) {
  125. try {
  126. await axios.post(`/weight/${row.md5}`, {
  127. weight: row.weight
  128. })
  129. this.$message.success('权重保存成功')
  130. } catch (error) {
  131. this.$message.error('保存失败')
  132. console.error('保存权重失败:', error)
  133. }
  134. },
  135. async getRandomVideo(forceNew = true) {
  136. try {
  137. if (this.infiniteLoop && this.currentVideoMD5 && !forceNew) {
  138. return this.playCurrent()
  139. }
  140. const res = this.getWeightedRandom(this.filteredFiles);
  141. this.currentVideoMD5 = res.md5
  142. this.currentVideo = `/play/${res.md5}`
  143. this.$nextTick(() => {
  144. const player = this.$refs.videoPlayer
  145. player.play().catch(error => {
  146. this.$message.warning('需要手动点击播放(浏览器限制)')
  147. })
  148. })
  149. } catch (error) {
  150. this.$message.error('获取视频失败')
  151. }
  152. },
  153. // 修改后的播放当前视频方法
  154. playCurrent() {
  155. const player = this.$refs.videoPlayer;
  156. // 直接操作播放器状态而不是修改src
  157. player.currentTime = 0;
  158. player.play().catch(error => {
  159. this.$message.warning('需要手动点击播放(浏览器限制)');
  160. });
  161. },
  162. handleVideoEnd() {
  163. if (this.infiniteLoop) {
  164. this.playCurrent()
  165. } else if (this.loopMode) {
  166. this.getRandomVideo(false)
  167. }
  168. },
  169. toggleInfiniteLoop() {
  170. if (this.infiniteLoop && !this.currentVideoMD5) {
  171. this.$message.warning('请先选择要循环的视频')
  172. this.infiniteLoop = false
  173. }
  174. }
  175. }
  176. })
  177. </script>
  178. </body>
  179. </html>