AudioLine.vue 12 KB

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