AudioLine.vue 12 KB

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