AudioLine.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <div :class="['Audio', mp3Source && mp3Source == 'tts' ? 'Audio-tts' : '']">
  3. <div v-if="!hideSlider" class="audioLine">
  4. <div
  5. class="play"
  6. :class="[
  7. audio.loading ? 'loadBtn' : audio.playing ? 'playBtn' : 'pauseBtn',
  8. ]"
  9. @click="PlayAudio"
  10. />
  11. <template v-if="!isRepeat">
  12. <el-slider
  13. v-model="playValue"
  14. :style="{ width: sliderWidth + 'px', height: '2px' }"
  15. :format-tooltip="formatProcessToolTip"
  16. @change="changeCurrentTime"
  17. />
  18. <span
  19. ><template v-if="audio.playing">-</template
  20. >{{
  21. audio.maxTime
  22. ? realFormatSecond(audio.maxTime - audio.currentTime)
  23. : ""
  24. }}</span
  25. >
  26. </template>
  27. </div>
  28. <div v-else class="audioLine2">
  29. <div
  30. class="play-icon"
  31. :class="
  32. audio.loading
  33. ? 'loadBtn'
  34. : audio.playing
  35. ? 'playBtn-icon'
  36. : 'pauseBtn-icon'
  37. "
  38. @click="PlayAudio"
  39. />
  40. </div>
  41. <audio
  42. :id="audioId"
  43. :ref="audioId"
  44. :src="mp3"
  45. preload="meta"
  46. @loadedmetadata="onLoadedmetadata"
  47. @timeupdate="onTimeupdate"
  48. @canplaythrough="oncanplaythrough"
  49. />
  50. </div>
  51. </template>
  52. <script>
  53. // 这里可以导入其它文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  54. // 例如:import 《组件名称》from ‘《组件路径》';
  55. export default {
  56. // import引入的组件需要注入到对象中才能使用
  57. components: {},
  58. props: [
  59. "mp3",
  60. "mp3Source",
  61. "getCurTime",
  62. "stopAudio",
  63. "width",
  64. "isRepeat",
  65. "themeColor",
  66. "hideSlider",
  67. "ed",
  68. "bg",
  69. "audioId",
  70. ],
  71. data() {
  72. // 这里存放数据
  73. return {
  74. playValue: 0,
  75. audio: {
  76. // 该字段是音频是否处于播放状态的属性
  77. playing: false,
  78. // 音频当前播放时长
  79. currentTime: 0,
  80. // 音频最大播放时长
  81. maxTime: 0,
  82. isPlaying: false,
  83. loading: false,
  84. },
  85. audioAllTime: null, // 展示总时间
  86. duioCurrentTime: null, // 剩余时间
  87. count: 0,
  88. loading: null,
  89. };
  90. },
  91. // 计算属性 类似于data概念
  92. computed: {
  93. sliderWidth() {
  94. let width = 0;
  95. if (this.width) {
  96. width = this.width;
  97. } else {
  98. width = 662;
  99. }
  100. return width;
  101. },
  102. },
  103. // 监控data中数据变化
  104. watch: {
  105. stopAudio: {
  106. handler(val, oldVal) {
  107. const _this = this;
  108. if (val) {
  109. _this.$refs[_this.audioId].pause();
  110. _this.audio.playing = false;
  111. }
  112. },
  113. // 深度观察监听
  114. deep: true,
  115. },
  116. "audio.playing": {
  117. handler(val) {
  118. this.$emit("playChange", val);
  119. if (val) this.$emit("handleChangeStopAudio");
  120. },
  121. },
  122. },
  123. // 生命周期 - 创建完成(可以访问当前this实例)
  124. created() {},
  125. // 生命周期 - 挂载完成(可以访问DOM元素)
  126. mounted() {
  127. let _this = this;
  128. let audioId = _this.audioId;
  129. _this.$refs[audioId].addEventListener("loadstart", function () {
  130. _this.loading = _this.$loading({
  131. lock: true,
  132. text: "Loading",
  133. spinner: "el-icon-loading",
  134. background: "rgba(0, 0, 0, 0.7)",
  135. });
  136. });
  137. _this.$refs[audioId].addEventListener("play", function () {
  138. _this.audio.playing = true;
  139. _this.audio.isPlaying = true;
  140. _this.audio.loading = false;
  141. });
  142. _this.$refs[audioId].addEventListener("pause", function () {
  143. _this.audio.playing = false;
  144. if (_this.hideSlider && _this.audio.currentTime * 1000 + 500 > _this.ed) {
  145. _this.$emit("sentPause", true);
  146. }
  147. });
  148. _this.$refs[audioId].addEventListener("ended", function () {
  149. _this.audio.playing = false;
  150. _this.audio.isPlaying = false;
  151. _this.$emit("handleListenRead", false);
  152. });
  153. this.$nextTick(() => {
  154. document
  155. .getElementsByClassName("el-slider__button-wrapper")[0]
  156. .addEventListener("mousedown", function () {
  157. _this.$refs[audioId].pause();
  158. _this.audio.playing = false;
  159. });
  160. });
  161. },
  162. // 生命周期-挂载之前
  163. beforeMount() {},
  164. // 生命周期-更新之后
  165. updated() {},
  166. // 如果页面有keep-alive缓存功能,这个函数会触发
  167. activated() {},
  168. // 方法集合
  169. methods: {
  170. PlayAudio() {
  171. let audioId = this.audioId;
  172. let audio = document.getElementsByTagName("audio");
  173. audio.forEach((item) => {
  174. if (item.src == this.mp3) {
  175. if (item.id !== audioId) {
  176. item.pause();
  177. }
  178. } else {
  179. item.pause();
  180. }
  181. });
  182. let video = document.getElementsByTagName("video");
  183. video.forEach((vItem) => {
  184. vItem.pause();
  185. });
  186. if (this.audio.playing) {
  187. this.$refs[audioId].pause();
  188. this.audio.playing = false;
  189. this.$emit("handleListenRead", false);
  190. } else {
  191. if (this.count == 0) {
  192. this.audio.loading = true;
  193. this.count++;
  194. }
  195. if (this.hideSlider) {
  196. this.$refs[audioId].play();
  197. this.onTimeupdateTime(this.bg / 1000);
  198. } else {
  199. this.$refs[audioId].play();
  200. }
  201. this.$emit("handleChangeStopAudio");
  202. this.$emit("handleListenRead", true);
  203. }
  204. },
  205. oncanplaythrough() {
  206. let _this = this;
  207. // setTimeout(() => {
  208. _this.audio.loading = false;
  209. _this.loading.close();
  210. // }, 10000);
  211. },
  212. // 点击 拖拽播放音频
  213. changeCurrentTime(value) {
  214. let audioId = this.audioId;
  215. this.$refs[audioId].play();
  216. this.audio.playing = true;
  217. this.$refs[audioId].currentTime = parseInt(
  218. (value / 100) * this.audio.maxTime
  219. );
  220. },
  221. mousedown() {
  222. let audioId = this.audioId;
  223. this.$refs[audioId].pause();
  224. this.audio.playing = false;
  225. },
  226. // 进度条格式化toolTip
  227. formatProcessToolTip(index) {
  228. index = parseInt((this.audio.maxTime / 100) * index);
  229. return this.realFormatSecond(index);
  230. },
  231. // 音频加载完之后
  232. onLoadedmetadata(res) {
  233. this.audio.maxTime = parseInt(res.target.duration);
  234. this.audioAllTime = this.realFormatSecond(this.audio.maxTime);
  235. },
  236. // 当音频当前时间改变后,进度条也要改变
  237. onTimeupdate(res) {
  238. let audioId = this.audioId;
  239. this.audio.currentTime = res.target.currentTime;
  240. this.getCurTime(res.target.currentTime);
  241. this.playValue = (this.audio.currentTime / this.audio.maxTime) * 100;
  242. if (this.audio.currentTime * 1000 > this.ed) {
  243. this.$refs[audioId].pause();
  244. }
  245. },
  246. onTimeupdateTime(res, playFlag) {
  247. if (!res) return;
  248. let audioId = this.audioId;
  249. this.$refs[audioId].currentTime = res;
  250. this.playValue = (res / this.audio.maxTime) * 100;
  251. if (playFlag) {
  252. let audio = document.getElementsByTagName("audio");
  253. audio.forEach((item) => {
  254. if (item.id !== audioId) {
  255. item.pause();
  256. }
  257. });
  258. this.$refs[audioId].play();
  259. }
  260. },
  261. // 将整数转换成 时:分:秒的格式
  262. realFormatSecond(value) {
  263. let theTime = parseInt(value); // 秒
  264. let theTime1 = 0; // 分
  265. let theTime2 = 0; // 小时
  266. if (theTime > 60) {
  267. theTime1 = parseInt(theTime / 60);
  268. theTime = parseInt(theTime % 60);
  269. if (theTime1 > 60) {
  270. theTime2 = parseInt(theTime1 / 60);
  271. theTime1 = parseInt(theTime1 % 60);
  272. }
  273. }
  274. let result = String(parseInt(theTime));
  275. if (result < 10) {
  276. result = "0" + result;
  277. }
  278. if (theTime1 > 0) {
  279. result = String(parseInt(theTime1)) + ":" + result;
  280. if (theTime1 < 10) {
  281. result = "0" + result;
  282. }
  283. } else {
  284. result = "00:" + result;
  285. }
  286. if (theTime2 > 0) {
  287. result = String(parseInt(theTime2)) + ":" + result;
  288. if (theTime2 < 10) {
  289. result = "0" + result;
  290. }
  291. } else {
  292. // result = "00:" + result;
  293. }
  294. return result;
  295. },
  296. },
  297. // 生命周期-创建之前
  298. beforeCreated() {},
  299. // 生命周期-更新之前
  300. beforUpdate() {},
  301. // 生命周期-销毁之前
  302. beforeDestory() {},
  303. // 生命周期-销毁完成
  304. destoryed() {},
  305. };
  306. </script>
  307. <style lang="scss" scoped>
  308. /* @import url(); 引入css类 */
  309. .Audio {
  310. width: 100%;
  311. .audioLine {
  312. display: flex;
  313. align-items: center;
  314. width: 100%;
  315. height: 40px;
  316. background: #ffffff;
  317. // border: 1px solid rgba(0, 0, 0, 0.1);
  318. // box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.1);
  319. box-sizing: border-box;
  320. border-radius: 4px;
  321. .play {
  322. margin-right: 12px;
  323. margin-left: 8px;
  324. width: 16px;
  325. min-width: 16px;
  326. height: 16px;
  327. cursor: pointer;
  328. display: block;
  329. }
  330. span {
  331. font-size: 16px;
  332. line-height: 19px;
  333. color: #000;
  334. margin-left: 8px;
  335. margin-right: 12px;
  336. min-width: 56px;
  337. text-align: right;
  338. }
  339. }
  340. .audioLine2 {
  341. .play-icon {
  342. width: 16px;
  343. height: 16px;
  344. cursor: pointer;
  345. &.playBtn-icon {
  346. background: url("../../../assets/icon/pauseC-16-normal-red.png")
  347. no-repeat left top;
  348. background-size: 100% 100%;
  349. }
  350. &.pauseBtn-icon {
  351. background: url("../../../assets/NPC/compare-pause-red.png") no-repeat
  352. left top;
  353. background-size: 100% 100%;
  354. }
  355. }
  356. }
  357. .loadBtn {
  358. background: url("../../../assets/NPC/loading-red.png") no-repeat left top;
  359. background-size: 100% 100%;
  360. }
  361. }
  362. </style>
  363. <style lang="scss">
  364. .Audio {
  365. .el-slider__button-wrapper {
  366. position: relative;
  367. z-index: 0;
  368. }
  369. .el-slider__button {
  370. width: 8px;
  371. height: 8px;
  372. top: 12px;
  373. position: absolute;
  374. }
  375. .el-slider__runway {
  376. margin: 0;
  377. padding: 0;
  378. background: #e5e5e5;
  379. border-radius: 0px;
  380. height: 2px;
  381. }
  382. .el-slider {
  383. position: relative;
  384. }
  385. .el-slider__bar {
  386. height: 2px;
  387. background: rgba(118, 99, 236, 1);
  388. }
  389. .el-slider__button {
  390. background: rgba(118, 99, 236, 1);
  391. border: none;
  392. }
  393. }
  394. .NPC-Book-Sty {
  395. .Audio {
  396. .el-slider__bar {
  397. height: 2px;
  398. background: #de4444;
  399. }
  400. .el-slider__button {
  401. background: #de4444;
  402. border: none;
  403. }
  404. }
  405. }
  406. .NPC-Big-Book-preview-green {
  407. .Audio {
  408. .el-slider__bar {
  409. background: #24b99e !important;
  410. }
  411. .el-slider__button {
  412. background: #24b99e !important;
  413. }
  414. .audioLine2 {
  415. .play-icon {
  416. &.playBtn-icon {
  417. background: url("../../../assets/icon/pauseC-16-normal-Green.png")
  418. no-repeat left top;
  419. background-size: 100% 100%;
  420. }
  421. &.pauseBtn-icon {
  422. background: url("../../../assets/NPC/compare-pause-green.png")
  423. no-repeat left top;
  424. background-size: 100% 100%;
  425. }
  426. }
  427. }
  428. .loadBtn {
  429. background: url("../../../assets/NPC/loading-green.png") no-repeat left
  430. top;
  431. background-size: 100% 100%;
  432. }
  433. }
  434. }
  435. .NPC-Big-Book-preview-brown {
  436. .Audio {
  437. .el-slider__bar {
  438. background: #bd8865 !important;
  439. }
  440. .el-slider__button {
  441. background: #bd8865 !important;
  442. }
  443. .audioLine2 {
  444. .play-icon {
  445. &.playBtn-icon {
  446. background: url("../../../assets/icon/pauseC-16-normal-Brown.png")
  447. no-repeat left top;
  448. background-size: 100% 100%;
  449. }
  450. &.pauseBtn-icon {
  451. background: url("../../../assets/NPC/compare-pause-brown.png")
  452. no-repeat left top;
  453. background-size: 100% 100%;
  454. }
  455. }
  456. }
  457. .loadBtn {
  458. background: url("../../../assets/NPC/loading-brown.png") no-repeat left
  459. top;
  460. background-size: 100% 100%;
  461. }
  462. }
  463. }
  464. </style>