AudioPlay.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="audio-wrapper">
  3. <span
  4. :class="[url ? 'audio-play' : 'audio-play not-url']"
  5. :style="{
  6. padding: showSlider ? '4px 16px' : '',
  7. width: showSlider ? 'auto' : '',
  8. }"
  9. @click="playAudio"
  10. >
  11. <SvgIcon v-if="audio.paused" :size="audio.paused ? 20 : 14" :icon-class="iconClass" />
  12. <img
  13. v-else
  14. :src="
  15. themeColor === 'gray'
  16. ? require('../../../../../assets/voice-play-gray.png')
  17. : require('../../../../../assets/voice-play-white.png')
  18. "
  19. class="voice-play"
  20. />
  21. <template v-if="showSlider">
  22. <span class="audio-time">{{ secondFormatConversion(audio.current_time) }}</span>
  23. <el-slider
  24. v-model="play_value"
  25. class="audio-slider"
  26. :format-tooltip="formatProcessToolTip"
  27. @change="changeCurrentTime"
  28. />
  29. <span class="audio-time">{{ audio_allTime }}</span>
  30. </template>
  31. </span>
  32. <audio
  33. :id="fileId"
  34. :ref="fileId"
  35. :src="url"
  36. preload="metadata"
  37. @loadedmetadata="onLoadedmetadata"
  38. @timeupdate="onTimeupdate"
  39. @canplaythrough="oncanplaythrough"
  40. ></audio>
  41. </div>
  42. </template>
  43. <script>
  44. import { GetFileURLMap } from '@/api/app';
  45. import { secondFormatConversion } from '@/utils/transform';
  46. export default {
  47. name: 'AudioPlay',
  48. props: {
  49. fileId: {
  50. type: String,
  51. required: true,
  52. },
  53. themeColor: {
  54. type: String,
  55. default: '',
  56. },
  57. showSlider: {
  58. type: Boolean,
  59. default: false,
  60. },
  61. },
  62. data() {
  63. return {
  64. secondFormatConversion,
  65. url: '',
  66. audio: {
  67. paused: true,
  68. playing: false,
  69. // 音频当前播放时长
  70. current_time: 0,
  71. // 音频最大播放时长
  72. max_time: 0,
  73. isPlaying: false,
  74. loading: false,
  75. },
  76. play_value: 0,
  77. audio_allTime: null, // 展示总时间
  78. };
  79. },
  80. computed: {
  81. iconClass() {
  82. return this.audio.paused ? 'audio' : 'audio-stop';
  83. },
  84. },
  85. watch: {
  86. fileId: {
  87. handler(val) {
  88. if (!val) return;
  89. GetFileURLMap({ file_id_list: [val] }).then(({ url_map }) => {
  90. this.url = url_map[val];
  91. });
  92. },
  93. immediate: true,
  94. },
  95. },
  96. mounted() {
  97. if (!this.fileId) return;
  98. this.$refs[this.fileId].addEventListener('ended', () => {
  99. this.audio.paused = true;
  100. });
  101. this.$refs[this.fileId].addEventListener('pause', () => {
  102. this.audio.paused = true;
  103. });
  104. this.$refs[this.fileId].addEventListener('play', () => {
  105. this.audio.paused = false;
  106. });
  107. },
  108. methods: {
  109. playAudio() {
  110. if (!this.url) return;
  111. const audio = this.$refs[this.fileId];
  112. let audioArr = document.getElementsByTagName('audio');
  113. if (audioArr && audioArr.length > 0) {
  114. for (let i = 0; i < audioArr.length; i++) {
  115. if (audioArr[i].src === this.url) {
  116. if (audioArr[i].id !== this.fileId) {
  117. audioArr[i].pause();
  118. }
  119. } else {
  120. audioArr[i].pause();
  121. }
  122. }
  123. }
  124. audio.paused ? audio.play() : audio.pause();
  125. },
  126. // 进度条格式化toolTip
  127. formatProcessToolTip(index) {
  128. let indexs = parseInt((this.audio.max_time / 100) * index);
  129. return secondFormatConversion(indexs);
  130. },
  131. // 点击 拖拽播放音频
  132. changeCurrentTime(value) {
  133. let audioId = this.fileId;
  134. this.$refs[audioId].play();
  135. this.audio.playing = true;
  136. this.$refs[audioId].currentTime = parseInt((value / 100) * this.audio.max_time);
  137. },
  138. // 音频加载完之后
  139. onLoadedmetadata(res) {
  140. this.audio.max_time = parseInt(res.target.duration);
  141. this.audio_allTime = secondFormatConversion(this.audio.max_time);
  142. },
  143. // 当音频当前时间改变后,进度条也要改变
  144. onTimeupdate(res) {
  145. let audioId = this.fileId;
  146. this.audio.current_time = res.target.currentTime;
  147. this.play_value = (this.audio.current_time / this.audio.max_time) * 100;
  148. if (this.audio.current_time * 1000 > this.ed) {
  149. this.$refs[audioId].pause();
  150. }
  151. },
  152. onTimeupdateTime(res, playFlag) {
  153. if (!res && res !== 0) return;
  154. let audioId = this.audioId;
  155. this.$refs[audioId].currentTime = res;
  156. this.play_value = (res / this.audio.max_time) * 100;
  157. if (playFlag) {
  158. let audio = document.getElementsByTagName('audio');
  159. audio.forEach((item) => {
  160. if (item.id !== audioId) {
  161. item.pause();
  162. }
  163. });
  164. this.$refs[audioId].play();
  165. }
  166. },
  167. oncanplaythrough() {
  168. this.audio.loading = false;
  169. },
  170. },
  171. };
  172. </script>
  173. <style lang="scss" scoped>
  174. .audio-wrapper {
  175. .audio-play {
  176. display: flex;
  177. column-gap: 8px;
  178. align-items: center;
  179. justify-content: center;
  180. width: 40px;
  181. height: 40px;
  182. color: #fff;
  183. cursor: pointer;
  184. background-color: $main-color;
  185. border-radius: 20px;
  186. &.not-url {
  187. color: #a1a1a1;
  188. cursor: not-allowed;
  189. }
  190. .voice-play {
  191. width: 20px;
  192. height: 20px;
  193. }
  194. .audio-time {
  195. min-width: 35px;
  196. font-size: 12px;
  197. font-weight: 400;
  198. line-height: 20px;
  199. }
  200. .audio-slider {
  201. width: 82px;
  202. :deep .el-slider__runway {
  203. height: 4px;
  204. background-color: #1853c6;
  205. }
  206. :deep .el-slider__button {
  207. width: 4px;
  208. height: 4px;
  209. border: none;
  210. }
  211. :deep .el-slider__bar {
  212. height: 4px;
  213. background-color: #fff;
  214. }
  215. :deep .el-slider__button-wrapper {
  216. top: -16px;
  217. }
  218. }
  219. }
  220. }
  221. </style>