index.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>视频随机播放器</title>
  6. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/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;"
  35. @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://unpkg.com/vue@2/dist/vue.js"></script>
  56. <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  57. <script src="https://unpkg.com/axios/dist/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. const totalWeight = files.reduce((sum, file) => sum + (file.weight || 1), 0);
  94. const random = Math.random() * totalWeight;
  95. let currentSum = 0;
  96. for (const file of files) {
  97. currentSum += file.weight || 1;
  98. if (random <= currentSum) return file;
  99. }
  100. return files[Math.floor(Math.random() * files.length)];
  101. }, async fetchFiles() {
  102. const res = await axios.get('/files')
  103. this.files = res.data
  104. },
  105. // 播放选中视频(新增)
  106. playSelected(row) {
  107. this.currentVideoMD5 = row.md5;
  108. this.currentVideo = `/play/${row.md5}?t=${Date.now()}`;
  109. this.$nextTick(() => {
  110. const player = this.$refs.videoPlayer;
  111. player.pause();
  112. player.load(); // 重新加载新源
  113. player.onloadeddata = () => {
  114. player.play().catch(error => {
  115. this.$message.warning('需要手动点击播放(浏览器限制)');
  116. });
  117. };
  118. });
  119. },
  120. // 权重更新(修改后)
  121. async updateWeight(row) {
  122. try {
  123. await axios.post(`/weight/${row.md5}`, {
  124. weight: row.weight
  125. })
  126. this.$message.success('权重保存成功')
  127. } catch (error) {
  128. this.$message.error('保存失败')
  129. console.error('保存权重失败:', error)
  130. }
  131. },
  132. async getRandomVideo(forceNew = true) {
  133. try {
  134. if (this.infiniteLoop && this.currentVideoMD5 && !forceNew) {
  135. return this.playCurrent()
  136. }
  137. const res = this.getWeightedRandom(this.filteredFiles);
  138. this.currentVideoMD5 = res.md5
  139. this.currentVideo = `/play/${res.md5}`
  140. this.$nextTick(() => {
  141. const player = this.$refs.videoPlayer
  142. player.play().catch(error => {
  143. this.$message.warning('需要手动点击播放(浏览器限制)')
  144. })
  145. })
  146. } catch (error) {
  147. this.$message.error('获取视频失败')
  148. }
  149. },
  150. // 修改后的播放当前视频方法
  151. playCurrent() {
  152. const player = this.$refs.videoPlayer;
  153. // 直接操作播放器状态而不是修改src
  154. player.currentTime = 0;
  155. player.play().catch(error => {
  156. this.$message.warning('需要手动点击播放(浏览器限制)');
  157. });
  158. },
  159. handleVideoEnd() {
  160. if (this.infiniteLoop) {
  161. this.playCurrent()
  162. } else if (this.loopMode) {
  163. this.getRandomVideo(false)
  164. }
  165. },
  166. toggleInfiniteLoop() {
  167. if (this.infiniteLoop && !this.currentVideoMD5) {
  168. this.$message.warning('请先选择要循环的视频')
  169. this.infiniteLoop = false
  170. }
  171. }
  172. }
  173. })
  174. </script>
  175. </body>
  176. </html>