index.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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-switch v-model="loopMode" active-text="自动连播" inactive-text="单次播放"></el-switch>
  16. <el-switch v-model="infiniteLoop" active-text="无限循环" inactive-text="正常模式"
  17. @change="toggleInfiniteLoop" style="flex-shrink: 0;">
  18. </el-switch>
  19. <div v-if="currentFile" style="display: flex; align-items: center; flex-shrink: 0;">
  20. <span style="margin-right: 10px;">当前播放:</span>
  21. <el-tag type="info">{{ currentFile.filename }}</el-tag>
  22. <span style="margin-left: 15px; margin-right: 10px;">权重:</span>
  23. <el-input-number v-model="currentFile.weight" :min="0" :max="10" size="mini"
  24. @change="updateWeight(currentFile)" style="width: 100px;">
  25. </el-input-number>
  26. </div>
  27. </div>
  28. <video ref="videoPlayer" controls :src="currentVideo" style="width: 100%; max-height: 80vh;"
  29. @ended="handleVideoEnd">
  30. 您的浏览器不支持视频播放
  31. </video>
  32. </el-main>
  33. <!-- 视频列表 -->
  34. <el-aside width="450px" style="background: #f5f5f5; height: 100vh;">
  35. <h3 style="margin-bottom: 15px;">视频列表(双击播放)</h3>
  36. <el-table :data="files" stripe height="calc(100vh - 60px)" @row-dblclick="playSelected">
  37. <el-table-column prop="filename" label="文件名"></el-table-column>
  38. <el-table-column label="权重" width="150">
  39. <template slot-scope="{row}">
  40. <el-input-number v-model="row.weight" :min="0" :max="10" @change="updateWeight(row)"
  41. size="mini">
  42. </el-input-number>
  43. </template>
  44. </el-table-column>
  45. </el-table>
  46. </el-aside>
  47. </el-container>
  48. </div>
  49. <script src="https://unpkg.com/vue@2/dist/vue.js"></script>
  50. <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  51. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  52. <script>
  53. new Vue({
  54. el: '#app',
  55. data() {
  56. return {
  57. files: [],
  58. currentVideo: '',
  59. currentVideoMD5: '',
  60. loopMode: true,
  61. infiniteLoop: false
  62. }
  63. },
  64. mounted() {
  65. this.fetchFiles()
  66. },
  67. computed: {
  68. currentFile() {
  69. return this.files.find(file => file.md5 === this.currentVideoMD5)
  70. }
  71. },
  72. methods: {
  73. async fetchFiles() {
  74. const res = await axios.get('/files')
  75. this.files = res.data
  76. },
  77. // 播放选中视频(新增)
  78. playSelected(row) {
  79. this.currentVideoMD5 = row.md5;
  80. this.currentVideo = `/play/${row.md5}?t=${Date.now()}`;
  81. this.$nextTick(() => {
  82. const player = this.$refs.videoPlayer;
  83. player.pause();
  84. player.load(); // 重新加载新源
  85. player.onloadeddata = () => {
  86. player.play().catch(error => {
  87. this.$message.warning('需要手动点击播放(浏览器限制)');
  88. });
  89. };
  90. });
  91. },
  92. // 权重更新(修改后)
  93. async updateWeight(row) {
  94. try {
  95. await axios.post(`/weight/${row.md5}`, {
  96. weight: row.weight
  97. })
  98. this.$message.success('权重保存成功')
  99. } catch (error) {
  100. this.$message.error('保存失败')
  101. console.error('保存权重失败:', error)
  102. }
  103. },
  104. async getRandomVideo(forceNew = true) {
  105. try {
  106. if (this.infiniteLoop && this.currentVideoMD5 && !forceNew) {
  107. return this.playCurrent()
  108. }
  109. const res = await axios.get('/random')
  110. this.currentVideoMD5 = res.data.md5
  111. this.currentVideo = `/play/${res.data.md5}`
  112. this.$nextTick(() => {
  113. const player = this.$refs.videoPlayer
  114. player.play().catch(error => {
  115. this.$message.warning('需要手动点击播放(浏览器限制)')
  116. })
  117. })
  118. } catch (error) {
  119. this.$message.error('获取视频失败')
  120. }
  121. },
  122. // 修改后的播放当前视频方法
  123. playCurrent() {
  124. const player = this.$refs.videoPlayer;
  125. // 直接操作播放器状态而不是修改src
  126. player.currentTime = 0;
  127. player.play().catch(error => {
  128. this.$message.warning('需要手动点击播放(浏览器限制)');
  129. });
  130. },
  131. handleVideoEnd() {
  132. if (this.infiniteLoop) {
  133. this.playCurrent()
  134. } else if (this.loopMode) {
  135. this.getRandomVideo(false)
  136. }
  137. },
  138. toggleInfiniteLoop() {
  139. if (this.infiniteLoop && !this.currentVideoMD5) {
  140. this.$message.warning('请先选择要循环的视频')
  141. this.infiniteLoop = false
  142. }
  143. }
  144. }
  145. })
  146. </script>
  147. </body>
  148. </html>