Audio.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div class="Audio">
  3. <audio id="audioTag" :src="audioSrc"></audio>
  4. <div class="audiocon">
  5. <!-- 按钮 -->
  6. <div class="leftBtn">
  7. <div class="play">
  8. <img @click="bofang" :src="require(`@/assets/img/goods/${isPlay ? 'audio2' : 'audio1'}.png`)" alt="" />
  9. </div>
  10. </div>
  11. <div class="adcon">
  12. <div class="bar">
  13. <div class="activeLine" @click="seekTime">
  14. <div :style="{ width: currentPosi + '%' }" class="dot"></div>
  15. </div>
  16. </div>
  17. <div class="time">
  18. <span>{{ time }}</span><span>&nbsp;/&nbsp;{{ allTime }}</span>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. name: "Audio",
  27. props: {
  28. audioSrc: {
  29. type: String,
  30. },
  31. },
  32. data() {
  33. return {
  34. active: 0,
  35. time: 0,
  36. _audio: "",
  37. isPlay: this.isMobile,
  38. currentPosi: 0,
  39. allTime: 0,
  40. };
  41. },
  42. methods: {
  43. bofang() {
  44. if (this._audio.paused) {
  45. this._audio.play();
  46. this.isPlay = true;
  47. } else {
  48. this._audio.pause();
  49. this.isPlay = false;
  50. }
  51. },
  52. transTime(time) {
  53. var duration = parseInt(time);
  54. var minute = parseInt(duration / 60);
  55. var sec = (duration % 60) + "";
  56. var isM0 = ":";
  57. if (minute == 0) {
  58. minute = "00";
  59. } else if (minute < 10) {
  60. minute = "0" + minute;
  61. }
  62. if (sec.length == 1) {
  63. sec = "0" + sec;
  64. }
  65. return minute + isM0 + sec;
  66. },
  67. updateProgress() {
  68. this.currentPosi = (this._audio.currentTime / this._audio.duration) * 100;
  69. this.time = this.transTime(this._audio.currentTime);
  70. },
  71. audioEnded() {
  72. this._audio.currentTime = 0;
  73. this._audio.pause();
  74. this.isPlay = false;
  75. },
  76. seekTime(e) {
  77. var rate = e.offsetX / e.target.clientWidth;
  78. this._audio.currentTime = this._audio.duration * rate;
  79. this.updateProgress();
  80. },
  81. },
  82. mounted() {
  83. this.$nextTick(() => {
  84. this._audio = $(`#audioTag`)[0];
  85. $(`#audioTag`).on("loadedmetadata", (e) => {
  86. this.time = this.isMobile
  87. ? "00:00"
  88. : this.transTime(e.currentTarget.duration);
  89. this.allTime = this.transTime(e.currentTarget.duration);
  90. this._audio.play();
  91. this.isPlay = true;
  92. });
  93. document.addEventListener(
  94. "WeixinJSBridgeReady",
  95. function () {
  96. this._audio.play();
  97. },
  98. false
  99. );
  100. $(`#audioTag`).on("timeupdate", () => {
  101. this.updateProgress();
  102. });
  103. $(`#audioTag`).on("timeupdate", () => {
  104. this.updateProgress();
  105. });
  106. $(`#audioTag`).on("ended", () => {
  107. this.audioEnded();
  108. });
  109. });
  110. },
  111. activated() {
  112. this.isPlay = this.isMobile;
  113. },
  114. };
  115. </script>
  116. <style lang="less" scoped>
  117. .Audio {
  118. width: 100%;
  119. height: 100%;
  120. padding: 0 15px;
  121. .audiocon {
  122. display: flex;
  123. align-items: center;
  124. width: 100%;
  125. height: 100%;
  126. justify-content: space-between;
  127. }
  128. .leftBtn {
  129. width: 30px;
  130. height: 30px;
  131. img {
  132. width: 100%;
  133. }
  134. }
  135. .adcon {
  136. position: relative;
  137. z-index: 10;
  138. width: calc(100% - 46px);
  139. height: 100%;
  140. display: flex;
  141. align-items: center;
  142. .bar {
  143. width: calc(100% - 80px);
  144. position: relative;
  145. height: 100%;
  146. display: flex;
  147. align-items: center;
  148. .activeLine {
  149. position: absolute;
  150. top: 0;
  151. left: 0px;
  152. z-index: 10;
  153. width: 96%;
  154. height: 100%;
  155. cursor: pointer;
  156. &::before {
  157. content: "";
  158. position: absolute;
  159. width: 100%;
  160. height: 4px;
  161. top: 50%;
  162. left: 0;
  163. transform: translateY(-50%);
  164. border-radius: 4px;
  165. background-color: #FFB199;
  166. }
  167. }
  168. .dot {
  169. border-radius: 4px;
  170. z-index: 11;
  171. pointer-events: none;
  172. position: absolute;
  173. height: 4px;
  174. top: 50%;
  175. left: 0px;
  176. transform: translateY(-50%);
  177. background-color: #FF615C;
  178. &::before {
  179. content: "";
  180. position: absolute;
  181. top: -3px;
  182. right: -3px;
  183. width: 10px;
  184. height: 10px;
  185. border-radius: 50%;
  186. background-color: #FF615C;
  187. }
  188. }
  189. }
  190. .time {
  191. width: 80px;
  192. font-size: 14px;
  193. color: #999999;
  194. display: flex;
  195. }
  196. }
  197. }
  198. </style>