Audio.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!-- -->
  2. <template>
  3. <div
  4. @click="handlePlayVoice"
  5. :class="fontSize == 20 ? 'questionSmall' : 'content-voice'"
  6. v-if="pinyin || mp3"
  7. >
  8. <span :class="mp3 ? '' : 'noMp3'" v-if="pinyin">{{ pinyin }}</span>
  9. <template v-if="mp3">
  10. <img :src="voiceSrc" />
  11. <!-- <audio :src="mp3" ref="curMp3"></audio> -->
  12. </template>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. components: {},
  18. props: ["pinyin", "mp3", "fontSize"],
  19. data() {
  20. return {
  21. audio: new Audio(),
  22. voiceSrc: "",
  23. voicePauseSrc: require("../../assets/single/icon-voice.png"),
  24. voicePlaySrc: require("../../assets/common/icon-voice-play.png"),
  25. };
  26. },
  27. computed: {},
  28. watch: {},
  29. //方法集合
  30. methods: {
  31. handlePlayVoice() {
  32. let _this = this;
  33. if (!_this.mp3) {
  34. return;
  35. }
  36. if (!this.audio.paused) {
  37. this.audio.pause();
  38. } else {
  39. _this.audio.pause();
  40. _this.audio.load();
  41. _this.audio.src = _this.mp3;
  42. _this.audio.loop = false;
  43. _this.audio.play();
  44. }
  45. },
  46. stopAudio() {
  47. if (this.audio) {
  48. this.audio.pause();
  49. }
  50. },
  51. },
  52. //生命周期 - 创建完成(可以访问当前this实例)
  53. created() {
  54. var that = this;
  55. window.stopAudioAudio = function () {
  56. if (that.audio) {
  57. that.audio.pause();
  58. }
  59. };
  60. },
  61. //生命周期 - 挂载完成(可以访问DOM元素)
  62. mounted() {
  63. let _this = this;
  64. _this.voiceSrc = _this.voicePauseSrc;
  65. _this.audio.addEventListener("play", function () {
  66. console.log("播放");
  67. _this.voiceSrc = _this.voicePlaySrc;
  68. });
  69. _this.audio.addEventListener("pause", function () {
  70. _this.voiceSrc = _this.voicePauseSrc;
  71. });
  72. _this.audio.addEventListener("ended", function () {
  73. console.log("停止");
  74. _this.voiceSrc = _this.voicePauseSrc;
  75. });
  76. },
  77. beforeCreate() {}, //生命周期 - 创建之前
  78. beforeMount() {}, //生命周期 - 挂载之前
  79. beforeUpdate() {}, //生命周期 - 更新之前
  80. updated() {}, //生命周期 - 更新之后
  81. beforeDestroy() {}, //生命周期 - 销毁之前
  82. destroyed() {}, //生命周期 - 销毁完成
  83. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  84. };
  85. </script>
  86. <style lang='scss' scoped>
  87. //@import url(); 引入公共css类
  88. .content-voice {
  89. display: inline-block;
  90. padding: 12px 36px;
  91. background: #ffffff;
  92. border-radius: 36px;
  93. height: 72px;
  94. margin: 0 auto 24px auto;
  95. font-size: 0;
  96. cursor: pointer;
  97. box-sizing: border-box;
  98. &-20 {
  99. span {
  100. color: #404040;
  101. font-size: 40px;
  102. line-height: 48px;
  103. float: left;
  104. margin-right: 24px;
  105. font-family: GB-PINYINOK-B;
  106. &.noMp3 {
  107. margin-right: 0px;
  108. }
  109. }
  110. img {
  111. height: 48px;
  112. float: left;
  113. padding-top: 0px;
  114. }
  115. }
  116. span {
  117. color: #404040;
  118. font-size: 40px;
  119. line-height: 48px;
  120. float: left;
  121. margin-right: 24px;
  122. font-family: GB-PINYINOK-B;
  123. &.noMp3 {
  124. margin-right: 0px;
  125. }
  126. }
  127. img {
  128. height: 48px;
  129. float: left;
  130. padding-top: 0px;
  131. }
  132. }
  133. .questionMiddle {
  134. .content-voice {
  135. padding: 8px 24px;
  136. border-radius: 24px;
  137. height: 48px;
  138. margin: 0 auto 16px auto;
  139. span {
  140. font-size: 26px;
  141. line-height: 30px;
  142. margin-right: 16px;
  143. &.noMp3 {
  144. margin-right: 0px;
  145. }
  146. }
  147. img {
  148. height: 32px;
  149. }
  150. }
  151. }
  152. .questionSmall {
  153. .content-voice {
  154. padding: 6px 18px;
  155. border-radius: 18px;
  156. height: 36px;
  157. margin: 0 auto 12px auto;
  158. span {
  159. font-size: 20px;
  160. line-height: 24px;
  161. margin-right: 12px;
  162. &.noMp3 {
  163. margin-right: 0px;
  164. }
  165. }
  166. img {
  167. height: 24px;
  168. }
  169. }
  170. }
  171. </style>