AudioLine.vue 13 KB

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