AudioPlay.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <template>
  2. <div class="audio-wrapper">
  3. <div v-if="'big' === viewSize" class="audio-big">
  4. <div v-if="'list' === viewMethod">
  5. <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ curAudioIndex + 1 }}. {{ fileName }}</div>
  6. <div v-if="showSlider" class="slider-area">
  7. <span class="audio-time">{{ secondFormatConversion(audio.current_time) }}</span>
  8. <el-slider
  9. v-model="play_value"
  10. class="audio-slider"
  11. :format-tooltip="formatProcessToolTip"
  12. :style="{ '--slider-color': topicColor }"
  13. @change="changeCurrentTime"
  14. />
  15. <span class="audio-time">{{ audio_allTime }}</span>
  16. <el-select v-model="audio.playbackRate" style="width: 100px" @change="onSpeedChange">
  17. <el-option
  18. v-for="speed in playbackRateList.split(' ')"
  19. :key="speed"
  20. :value="speed"
  21. :selected="speed === 1.0"
  22. :label="`${speed}x`"
  23. >
  24. {{ speed }}x
  25. </el-option>
  26. </el-select>
  27. </div>
  28. <div class="audio-icon">
  29. <SvgIcon icon-class="pre" size="12" :color="topicColor" @click="changeFile('prev')" />
  30. <SvgIcon :icon-class="iconClass" :color="topicColor" size="26" @click="playAudio" />
  31. <SvgIcon icon-class="next" size="12" :color="topicColor" @click="changeFile('next')" />
  32. </div>
  33. </div>
  34. <div v-else class="independent-big">
  35. <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
  36. <div class="slider-area">
  37. <SvgIcon :icon-class="iconClass" size="18" :color="topicColor" style="cursor: pointer" @click="playAudio" />
  38. <span class="audio-time">{{ secondFormatConversion(audio.current_time) }}</span>
  39. <el-slider
  40. v-model="play_value"
  41. class="audio-slider"
  42. :format-tooltip="formatProcessToolTip"
  43. :style="{ '--slider-color': topicColor }"
  44. @change="changeCurrentTime"
  45. />
  46. <span class="audio-time">{{ audio_allTime }}</span>
  47. <el-select v-model="audio.playbackRate" style="width: 100px" @change="onSpeedChange">
  48. <el-option
  49. v-for="speed in playbackRateList.split(' ')"
  50. :key="speed"
  51. :value="speed"
  52. :selected="speed === 1.0"
  53. :label="`${speed}x`"
  54. >
  55. {{ speed }}x
  56. </el-option>
  57. </el-select>
  58. </div>
  59. </div>
  60. </div>
  61. <!-- <div v-else-if="'middle' === viewSize" class="audio-middle">
  62. <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
  63. <div class="slider-area">
  64. <SvgIcon :icon-class="iconClass" size="18" :color="topicColor" @click="playAudio" />
  65. <span class="audio-time">{{ secondFormatConversion(audio.current_time) }}</span>
  66. <el-slider
  67. v-model="play_value"
  68. class="audio-slider"
  69. :format-tooltip="formatProcessToolTip"
  70. :style="{ '--slider-color': topicColor }"
  71. @change="changeCurrentTime"
  72. />
  73. <span class="audio-time">{{ audio_allTime }}</span>
  74. </div>
  75. </div> -->
  76. <div v-else-if="'middle' === viewSize" class="audio-middle">
  77. <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
  78. <span class="icon-box" :style="{ borderColor: topicColor }">
  79. <SvgIcon
  80. :icon-class="iconClass"
  81. size="12"
  82. :color="topicColor"
  83. :style="{ marginLeft: audio.paused ? '4px' : '0' }"
  84. @click="playAudio"
  85. />
  86. </span>
  87. </div>
  88. <div v-else-if="'small' === viewSize" class="audio-small">
  89. <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
  90. <SvgIcon
  91. icon-class="sound"
  92. size="20"
  93. :color="topicColor"
  94. :class="{ 'sound-icon--playing': !audio.paused }"
  95. @click="playAudio"
  96. />
  97. </div>
  98. <div v-else :class="['audio-list', { active: audioIndex === curAudioIndex }]" :style="cssVars">
  99. <div class="active-mark">
  100. <SvgIcon
  101. v-if="audioIndex === curAudioIndex"
  102. icon-class="paused"
  103. size="12"
  104. :color="topicColor"
  105. style="cursor: default"
  106. />
  107. </div>
  108. <span class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</span>
  109. <span class="audio-time">{{ audio_allTime }}</span>
  110. </div>
  111. <audio
  112. :id="fileId"
  113. :ref="fileId"
  114. :src="fileUrl"
  115. preload="metadata"
  116. @loadedmetadata="onLoadedmetadata"
  117. @timeupdate="onTimeupdate"
  118. @canplaythrough="oncanplaythrough"
  119. ></audio>
  120. </div>
  121. </template>
  122. <script>
  123. import { GetFileURLMap } from '@/api/app';
  124. import { secondFormatConversion } from '@/utils/transform';
  125. export default {
  126. name: 'AudioPlay',
  127. props: {
  128. fileId: {
  129. type: String,
  130. required: true,
  131. },
  132. fileUrl: {
  133. type: String,
  134. default: '',
  135. },
  136. fileName: {
  137. type: String,
  138. default: '',
  139. },
  140. viewSize: {
  141. type: String,
  142. default: 'big',
  143. },
  144. viewMethod: {
  145. type: String,
  146. default: 'independent',
  147. },
  148. audioIndex: {
  149. type: Number,
  150. default: 0,
  151. },
  152. curAudioIndex: {
  153. type: Number,
  154. default: 0,
  155. },
  156. showSlider: {
  157. type: Boolean,
  158. default: false,
  159. },
  160. // 是否显示音频进度条
  161. showProgress: {
  162. type: Boolean,
  163. default: true,
  164. },
  165. fileNameDisplay: {
  166. type: String,
  167. default: 'true',
  168. },
  169. topicColor: {
  170. type: String,
  171. default: '#076aff',
  172. },
  173. assistColor: {
  174. type: String,
  175. default: '#076aff',
  176. },
  177. },
  178. data() {
  179. return {
  180. secondFormatConversion,
  181. url: '',
  182. audio: {
  183. paused: true,
  184. playing: false,
  185. // 音频当前播放时长
  186. current_time: 0,
  187. // 音频最大播放时长
  188. max_time: 0,
  189. isPlaying: false,
  190. loading: false,
  191. playbackRate: '1.0',
  192. },
  193. playbackRateList: '0.25 0.5 0.75 1.0 1.25 1.5 1.75 2.0 3.0',
  194. play_value: 0,
  195. audio_allTime: null, // 展示总时间
  196. };
  197. },
  198. computed: {
  199. iconClass() {
  200. return this.audio.paused ? 'paused' : 'playing';
  201. },
  202. cssVars() {
  203. return {
  204. '--assist-color': this.assistColor,
  205. };
  206. },
  207. },
  208. watch: {
  209. fileId: {
  210. handler(val) {
  211. if (this.fileUrl) {
  212. this.url = this.fileUrl;
  213. return;
  214. }
  215. if (!val) return;
  216. GetFileURLMap({ file_id_list: [val] }).then(({ url_map }) => {
  217. this.url = url_map[val];
  218. });
  219. },
  220. immediate: true,
  221. },
  222. },
  223. mounted() {
  224. if (this.fileId) {
  225. this.$refs[this.fileId].addEventListener('ended', () => {
  226. this.audio.paused = true;
  227. });
  228. this.$refs[this.fileId].addEventListener('pause', () => {
  229. this.audio.paused = true;
  230. });
  231. this.$refs[this.fileId].addEventListener('play', () => {
  232. this.audio.paused = false;
  233. });
  234. }
  235. },
  236. methods: {
  237. getCurTime(curTime) {
  238. this.curTime = curTime * 1000;
  239. },
  240. playAudio() {
  241. if (!this.url || !this.fileUrl) return;
  242. const audio = this.$refs[this.fileId] || {};
  243. this.pauseAll();
  244. audio.playbackRate = this.audio.playbackRate;
  245. audio.paused ? audio.play() : audio.pause();
  246. },
  247. onSpeedChange(speed) {
  248. const audio = this.$refs[this.fileId] || {};
  249. audio.playbackRate = this.audio.playbackRate;
  250. },
  251. // 进度条格式化toolTip
  252. formatProcessToolTip(index) {
  253. let _index = parseInt((this.audio.max_time / 100) * index);
  254. return secondFormatConversion(_index);
  255. },
  256. // 点击 拖拽播放音频
  257. changeCurrentTime(value) {
  258. this.pauseAll();
  259. this.$refs[this.fileId].play();
  260. this.audio.playing = true;
  261. this.$refs[this.fileId].currentTime = parseInt((value / 100) * this.audio.max_time);
  262. },
  263. // 音频加载完之后
  264. onLoadedmetadata(res) {
  265. this.audio.max_time = parseInt(res.target.duration);
  266. this.audio_allTime = secondFormatConversion(this.audio.max_time);
  267. },
  268. // 当音频当前时间改变后,进度条也要改变
  269. onTimeupdate(res) {
  270. this.audio.current_time = res.target.currentTime;
  271. this.play_value = (this.audio.current_time / this.audio.max_time) * 100;
  272. if (this.audio.current_time * 1000 > this.ed) {
  273. this.$refs[this.fileId].pause();
  274. }
  275. },
  276. onTimeupdateTime(res, playFlag) {
  277. if (!res && res !== 0) return;
  278. let audioId = this.audioId;
  279. this.$refs[audioId].currentTime = res;
  280. this.play_value = (res / this.audio.max_time) * 100;
  281. if (playFlag) {
  282. let audio = document.getElementsByTagName('audio');
  283. audio.forEach((item) => {
  284. if (item.id !== audioId) {
  285. item.pause();
  286. }
  287. });
  288. this.$refs[audioId].play();
  289. }
  290. },
  291. oncanplaythrough() {
  292. this.audio.loading = false;
  293. },
  294. pauseAll() {
  295. let audioArr = document.getElementsByTagName('audio');
  296. if (audioArr && audioArr.length > 0) {
  297. for (let i = 0; i < audioArr.length; i++) {
  298. if (audioArr[i].id !== this.fileId) {
  299. audioArr[i].pause();
  300. }
  301. }
  302. }
  303. },
  304. changeFile(type) {
  305. this.pauseAll();
  306. this.$emit('changeFile', type);
  307. },
  308. changeAudio(id) {
  309. this.pauseAll();
  310. },
  311. },
  312. };
  313. </script>
  314. <style lang="scss" scoped>
  315. .audio-wrapper {
  316. .audio-time {
  317. white-space: nowrap;
  318. }
  319. .audio-big {
  320. display: flex;
  321. flex-direction: column;
  322. padding: 0;
  323. overflow: hidden;
  324. text-align: center;
  325. .audio-name {
  326. padding: 8px;
  327. margin-bottom: 8px;
  328. font-size: 14px;
  329. background-color: #eee;
  330. border-radius: 4px;
  331. }
  332. .audio-icon {
  333. display: flex;
  334. column-gap: 32px;
  335. align-items: center;
  336. justify-content: center;
  337. .svg-icon {
  338. cursor: pointer;
  339. }
  340. }
  341. }
  342. .independent-big {
  343. .audio-name {
  344. text-align: left;
  345. }
  346. }
  347. .audio-middle1 {
  348. width: 280px;
  349. padding: 12px 16px;
  350. border-radius: 4px;
  351. .audio-name {
  352. padding: 8px;
  353. margin-bottom: 8px;
  354. font-size: 14px;
  355. text-align: left;
  356. background-color: #eee;
  357. border-radius: 4px;
  358. }
  359. }
  360. .audio-middle {
  361. display: flex;
  362. flex-direction: column;
  363. column-gap: 10px;
  364. align-items: left;
  365. margin-left: 5px;
  366. .audio-name {
  367. padding: 8px;
  368. margin-bottom: 8px;
  369. font-size: 14px;
  370. text-align: center;
  371. background-color: #eee;
  372. border-radius: 4px;
  373. }
  374. .icon-box {
  375. display: flex;
  376. align-items: center;
  377. justify-content: center;
  378. width: 32px;
  379. height: 32px;
  380. cursor: pointer;
  381. border: 4px solid #076aff;
  382. border-radius: 20px;
  383. // .svg-icon {
  384. // color: #fff;
  385. // }
  386. }
  387. }
  388. .audio-small {
  389. display: flex;
  390. flex-direction: column;
  391. column-gap: 10px;
  392. align-items: left;
  393. margin-left: 10px;
  394. cursor: pointer;
  395. .audio-name {
  396. padding: 8px;
  397. margin-bottom: 8px;
  398. font-size: 14px;
  399. text-align: center;
  400. background-color: #eee;
  401. border-radius: 4px;
  402. }
  403. }
  404. .audio-list {
  405. display: flex;
  406. align-items: center;
  407. justify-content: space-between;
  408. padding: 8px 24px 8px 16px;
  409. &.active {
  410. //background-color: #ebb572;
  411. background-color: var(--assist-color, #f4f9ff);
  412. }
  413. .active-mark {
  414. width: 20px;
  415. height: 28px;
  416. }
  417. .audio-name {
  418. flex: 1;
  419. }
  420. }
  421. .slider-area {
  422. display: flex;
  423. column-gap: 16px;
  424. align-items: center;
  425. justify-content: space-between;
  426. font-size: 14px;
  427. .audio-slider {
  428. width: 90%;
  429. :deep .el-slider__runway {
  430. height: 4px;
  431. }
  432. :deep .el-slider__button {
  433. width: 4px;
  434. height: 4px;
  435. margin-top: -1px;
  436. border: none;
  437. }
  438. :deep .el-slider__bar {
  439. height: 4px;
  440. background-color: var(--slider-color, #076aff);
  441. }
  442. }
  443. }
  444. :deep .el-input .el-input__inner {
  445. background-color: rgba(255, 255, 255, 0%);
  446. border: none;
  447. span {
  448. display: none !important;
  449. }
  450. }
  451. }
  452. .sound-icon--playing {
  453. animation: soundWave 1s infinite;
  454. }
  455. @keyframes soundWave {
  456. 0%,
  457. 100% {
  458. opacity: 1;
  459. transform: scale(1);
  460. }
  461. 50% {
  462. opacity: 0.5;
  463. transform: scale(1.2);
  464. }
  465. }
  466. </style>