AudioRed.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!-- -->
  2. <template>
  3. <div @click="handlePlayVoice" class="content-voices" v-if="mp3">
  4. <img :src="voiceSrc" />
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. components: {},
  10. props: ["seconds", "mp3", "wav"],
  11. data() {
  12. return {
  13. audio: new Audio(),
  14. voiceSrc: "",
  15. voicePauseSrc: require("../../assets/common/icon-voice-red.png"),
  16. voicePlaySrc: require("../../assets/common/icon-voice-red-play.png"),
  17. };
  18. },
  19. computed: {},
  20. watch: {},
  21. //方法集合
  22. methods: {
  23. handlePlayVoice() {
  24. let _this = this;
  25. let audio = document.getElementsByTagName("audio");
  26. if(audio&&audio.length>0&&window.location.href.indexOf('GCLS-Learn')==-1){
  27. audio.forEach((item) => {
  28. if (item.src != _this.mp3) {
  29. item.pause();
  30. }
  31. });
  32. }
  33. if (!_this.audio.paused) {
  34. _this.audio.pause();
  35. } else {
  36. let _this = _this;
  37. _this.audio.pause();
  38. _this.audio.load();
  39. _this.audio.src = _this.mp3;
  40. _this.audio.loop = false;
  41. _this.audio.play();
  42. }
  43. },
  44. stopAudio() {
  45. if (this.audio) {
  46. this.audio.pause();
  47. }
  48. },
  49. },
  50. //生命周期 - 创建完成(可以访问当前this实例)
  51. created() {
  52. var that = this;
  53. window.stopAudioVoice = function () {
  54. if (that.audio) {
  55. that.audio.pause();
  56. }
  57. };
  58. },
  59. //生命周期 - 挂载完成(可以访问DOM元素)
  60. mounted() {
  61. let _this = this;
  62. _this.voiceSrc = _this.voicePauseSrc;
  63. _this.audio.addEventListener("play", function () {
  64. console.log("播放");
  65. _this.voiceSrc = _this.voicePlaySrc;
  66. });
  67. _this.audio.addEventListener("pause", function () {
  68. _this.voiceSrc = _this.voicePauseSrc;
  69. });
  70. _this.audio.addEventListener("ended", function () {
  71. console.log("停止");
  72. _this.voiceSrc = _this.voicePauseSrc;
  73. });
  74. },
  75. beforeCreate() {}, //生命周期 - 创建之前
  76. beforeMount() {}, //生命周期 - 挂载之前
  77. beforeUpdate() {}, //生命周期 - 更新之前
  78. updated() {}, //生命周期 - 更新之后
  79. beforeDestroy() {}, //生命周期 - 销毁之前
  80. destroyed() {}, //生命周期 - 销毁完成
  81. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  82. };
  83. </script>
  84. <style lang='scss' scoped>
  85. //@import url(); 引入公共css类
  86. .content-voices {
  87. display: flex;
  88. align-items: center;
  89. justify-content: center;
  90. width: 100%;
  91. font-size: 0;
  92. cursor: pointer;
  93. span {
  94. color: #2c2c2c;
  95. font-size: 24px;
  96. line-height: 30px;
  97. float: left;
  98. font-family: sourceR;
  99. &.noMp3 {
  100. margin-left: 0px;
  101. }
  102. }
  103. img {
  104. height: 24px;
  105. float: left;
  106. }
  107. }
  108. .questionMiddle {
  109. .content-voices {
  110. span {
  111. font-size: 16px;
  112. line-height: 20px;
  113. &.noMp3 {
  114. margin-left: 0px;
  115. }
  116. }
  117. img {
  118. height: 16px;
  119. float: left;
  120. }
  121. }
  122. }
  123. .questionSmall {
  124. .content-voices {
  125. span {
  126. font-size: 12px;
  127. line-height: 15px;
  128. &.noMp3 {
  129. margin-left: 0px;
  130. }
  131. }
  132. img {
  133. height: 12px;
  134. float: left;
  135. }
  136. }
  137. }
  138. </style>