AudioLine.vue 12 KB

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