AudioPlay.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div class="audio-wrapper">
  3. <span :class="[url ? 'audio-play' : 'audio-play not-url']" @click="playAudio">
  4. <SvgIcon v-if="audio.paused" :size="audio.paused ? 20 : 14" :icon-class="iconClass" />
  5. <img
  6. v-else
  7. :src="
  8. themeColor === 'gray'
  9. ? require('../../../../../assets/voice-play-gray.png')
  10. : require('../../../../../assets/voice-play-white.png')
  11. "
  12. class="voice-play"
  13. />
  14. </span>
  15. <audio ref="audio" :src="url" preload="metadata"></audio>
  16. </div>
  17. </template>
  18. <script>
  19. import { GetFileURLMap } from '@/api/app';
  20. export default {
  21. name: 'AudioPlay',
  22. props: {
  23. fileId: {
  24. type: String,
  25. required: true,
  26. },
  27. themeColor: {
  28. type: String,
  29. default: '',
  30. },
  31. },
  32. data() {
  33. return {
  34. url: '',
  35. audio: {
  36. paused: true,
  37. },
  38. };
  39. },
  40. computed: {
  41. iconClass() {
  42. return this.audio.paused ? 'audio' : 'audio-stop';
  43. },
  44. },
  45. watch: {
  46. fileId: {
  47. handler(val) {
  48. if (!val) return;
  49. GetFileURLMap({ file_id_list: [val] }).then(({ url_map }) => {
  50. this.url = url_map[val];
  51. });
  52. },
  53. immediate: true,
  54. },
  55. },
  56. mounted() {
  57. this.$refs.audio.addEventListener('ended', () => {
  58. this.audio.paused = true;
  59. });
  60. this.$refs.audio.addEventListener('pause', () => {
  61. this.audio.paused = true;
  62. });
  63. this.$refs.audio.addEventListener('play', () => {
  64. this.audio.paused = false;
  65. });
  66. },
  67. methods: {
  68. playAudio() {
  69. if (!this.url) return;
  70. const audio = this.$refs.audio;
  71. audio.paused ? audio.play() : audio.pause();
  72. },
  73. },
  74. };
  75. </script>
  76. <style lang="scss" scoped>
  77. .audio-wrapper {
  78. .audio-play {
  79. display: flex;
  80. align-items: center;
  81. justify-content: center;
  82. width: 40px;
  83. height: 40px;
  84. color: #fff;
  85. cursor: pointer;
  86. background-color: $main-color;
  87. border-radius: 50%;
  88. &.not-url {
  89. color: #a1a1a1;
  90. cursor: not-allowed;
  91. }
  92. .voice-play {
  93. width: 20px;
  94. height: 20px;
  95. }
  96. }
  97. }
  98. </style>