VoiceMatrixFullscreenAudio.vue 11 KB

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