AudioLine.vue 13 KB

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