AudioLine.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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="metadata"
  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. console.log(res.target.duration);
  234. this.audio.maxTime = parseInt(res.target.duration);
  235. this.audioAllTime = this.realFormatSecond(this.audio.maxTime);
  236. },
  237. // 当音频当前时间改变后,进度条也要改变
  238. onTimeupdate(res) {
  239. let audioId = this.audioId;
  240. this.audio.currentTime = res.target.currentTime;
  241. this.getCurTime(res.target.currentTime);
  242. this.playValue = (this.audio.currentTime / this.audio.maxTime) * 100;
  243. if (this.type == "audioLine") {
  244. if (!this.isClick && this.audio.currentTime * 1000 > this.ed) {
  245. this.$refs[audioId].pause();
  246. this.$emit("emptyEd");
  247. }
  248. } else {
  249. if (this.audio.currentTime * 1000 > this.ed) {
  250. this.$refs[audioId].pause();
  251. }
  252. }
  253. },
  254. onTimeupdateTime(res, playFlag) {
  255. if (!res&&res!=0) return;
  256. let audioId = this.audioId;
  257. this.$refs[audioId].currentTime = res;
  258. this.playValue = (res / this.audio.maxTime) * 100;
  259. if (playFlag) {
  260. let audio = document.getElementsByTagName("audio");
  261. audio.forEach((item) => {
  262. if (item.id !== audioId) {
  263. item.pause();
  264. }
  265. });
  266. this.$refs[audioId].play();
  267. }
  268. },
  269. // 将整数转换成 时:分:秒的格式
  270. realFormatSecond(value) {
  271. let theTime = parseInt(value); // 秒
  272. let theTime1 = 0; // 分
  273. let theTime2 = 0; // 小时
  274. if (theTime > 60) {
  275. theTime1 = parseInt(theTime / 60);
  276. theTime = parseInt(theTime % 60);
  277. if (theTime1 > 60) {
  278. theTime2 = parseInt(theTime1 / 60);
  279. theTime1 = parseInt(theTime1 % 60);
  280. }
  281. }
  282. let result = String(parseInt(theTime));
  283. if (result < 10) {
  284. result = "0" + result;
  285. }
  286. if (theTime1 > 0) {
  287. result = String(parseInt(theTime1)) + ":" + result;
  288. if (theTime1 < 10) {
  289. result = "0" + result;
  290. }
  291. } else {
  292. result = "00:" + result;
  293. }
  294. if (theTime2 > 0) {
  295. result = String(parseInt(theTime2)) + ":" + result;
  296. if (theTime2 < 10) {
  297. result = "0" + result;
  298. }
  299. } else {
  300. // result = "00:" + result;
  301. }
  302. return result;
  303. },
  304. },
  305. // 生命周期-创建之前
  306. beforeCreated() {},
  307. // 生命周期-更新之前
  308. beforUpdate() {},
  309. // 生命周期-销毁之前
  310. beforeDestory() {},
  311. // 生命周期-销毁完成
  312. destoryed() {},
  313. };
  314. </script>
  315. <style lang="scss" scoped>
  316. /* @import url(); 引入css类 */
  317. .Audio {
  318. width: 100%;
  319. .audioLine {
  320. display: flex;
  321. align-items: center;
  322. width: 100%;
  323. height: 40px;
  324. background: #ffffff;
  325. // border: 1px solid rgba(0, 0, 0, 0.1);
  326. // box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.1);
  327. box-sizing: border-box;
  328. border-radius: 8px;
  329. &-bottomNone{
  330. border-radius: 8px 8px 0 0;
  331. }
  332. .play {
  333. margin-right: 12px;
  334. margin-left: 8px;
  335. width: 16px;
  336. min-width: 16px;
  337. height: 16px;
  338. cursor: pointer;
  339. display: block;
  340. }
  341. span {
  342. font-size: 16px;
  343. line-height: 19px;
  344. color: #000;
  345. margin-left: 8px;
  346. margin-right: 12px;
  347. min-width: 56px;
  348. text-align: right;
  349. }
  350. }
  351. .audioLine2 {
  352. .play-icon {
  353. width: 16px;
  354. height: 16px;
  355. cursor: pointer;
  356. &-big {
  357. width: 24px;
  358. height: 24px;
  359. }
  360. &.playBtn-icon {
  361. background: url("../../../assets/icon/pauseC-24-normal-red.png")
  362. no-repeat left top;
  363. background-size: 100% 100%;
  364. }
  365. &.pauseBtn-icon {
  366. background: url("../../../assets/NPC/compare-pause-red-24.png")
  367. no-repeat left top;
  368. background-size: 100% 100%;
  369. }
  370. }
  371. }
  372. .loadBtn {
  373. background: url("../../../assets/NPC/loading-red.png") no-repeat left top;
  374. background-size: 100% 100%;
  375. }
  376. }
  377. </style>
  378. <style lang="scss">
  379. .Audio {
  380. .el-slider__bar {
  381. height: 2px;
  382. background: #de4444;
  383. }
  384. .el-slider__button {
  385. background: #de4444;
  386. border: none;
  387. }
  388. .el-slider__button-wrapper {
  389. width: 25px;
  390. }
  391. .el-slider__button-wrapper {
  392. position: relative;
  393. z-index: 0;
  394. }
  395. .el-slider__button {
  396. width: 8px;
  397. height: 8px;
  398. top: 12px;
  399. position: absolute;
  400. }
  401. .el-slider__runway {
  402. margin: 0;
  403. padding: 0;
  404. background: #e5e5e5;
  405. border-radius: 0px;
  406. height: 2px;
  407. }
  408. .el-slider {
  409. position: relative;
  410. }
  411. }
  412. .NPC-Book-Sty {
  413. .Audio {
  414. .el-slider__bar {
  415. height: 2px;
  416. background: #de4444;
  417. }
  418. .el-slider__button {
  419. background: #de4444;
  420. border: none;
  421. }
  422. }
  423. }
  424. .NPC-Big-Book-preview-green {
  425. .Audio {
  426. .el-slider__bar {
  427. background: #24b99e !important;
  428. }
  429. .el-slider__button {
  430. background: #24b99e !important;
  431. }
  432. .audioLine2 {
  433. .play-icon {
  434. &.playBtn-icon {
  435. background: url("../../../assets/icon/pauseC-16-normal-Green.png")
  436. no-repeat left top;
  437. background-size: 100% 100%;
  438. }
  439. &.pauseBtn-icon {
  440. background: url("../../../assets/NPC/compare-pause-green.png")
  441. no-repeat left top;
  442. background-size: 100% 100%;
  443. }
  444. }
  445. }
  446. .loadBtn {
  447. background: url("../../../assets/NPC/loading-green.png") no-repeat left
  448. top;
  449. background-size: 100% 100%;
  450. }
  451. }
  452. }
  453. .NPC-Big-Book-preview-brown {
  454. .Audio {
  455. .el-slider__bar {
  456. background: #bd8865 !important;
  457. }
  458. .el-slider__button {
  459. background: #bd8865 !important;
  460. }
  461. .audioLine2 {
  462. .play-icon {
  463. &.playBtn-icon {
  464. background: url("../../../assets/icon/pauseC-16-normal-Brown.png")
  465. no-repeat left top;
  466. background-size: 100% 100%;
  467. }
  468. &.pauseBtn-icon {
  469. background: url("../../../assets/NPC/compare-pause-brown.png")
  470. no-repeat left top;
  471. background-size: 100% 100%;
  472. }
  473. }
  474. }
  475. .loadBtn {
  476. background: url("../../../assets/NPC/loading-brown.png") no-repeat left
  477. top;
  478. background-size: 100% 100%;
  479. }
  480. }
  481. }
  482. </style>