AudioLine.vue 12 KB

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