VoicePlay.vue 3.7 KB

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