AudioLine.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. },
  141. // 生命周期 - 创建完成(可以访问当前this实例)
  142. created() {},
  143. // 生命周期 - 挂载完成(可以访问DOM元素)
  144. mounted() {
  145. let _this = this;
  146. let audioId = _this.audioId;
  147. _this.$refs[audioId].addEventListener("loadstart", function () {});
  148. _this.$refs[audioId].addEventListener("play", function () {
  149. _this.audio.playing = true;
  150. _this.audio.isPlaying = true;
  151. _this.audio.loading = false;
  152. });
  153. _this.$refs[audioId].addEventListener("pause", function () {
  154. _this.audio.playing = false;
  155. _this.$emit("handleListenRead", false);
  156. });
  157. _this.$refs[audioId].addEventListener("ended", function () {
  158. _this.audio.playing = false;
  159. _this.audio.isPlaying = false;
  160. _this.$emit("handleListenRead", false);
  161. _this.isClick = false;
  162. });
  163. this.$nextTick(() => {
  164. document
  165. .getElementsByClassName("el-slider__button-wrapper")[0]
  166. .addEventListener("mousedown", function () {
  167. _this.$refs[audioId].pause();
  168. _this.audio.playing = false;
  169. });
  170. });
  171. },
  172. // 生命周期-挂载之前
  173. beforeMount() {},
  174. // 生命周期-更新之后
  175. updated() {},
  176. // 如果页面有keep-alive缓存功能,这个函数会触发
  177. activated() {},
  178. // 方法集合
  179. methods: {
  180. PlayAudio() {
  181. let audioId = this.audioId;
  182. let audio = document.getElementsByTagName("audio");
  183. audio.forEach((item) => {
  184. if (item.src == this.mp3) {
  185. if (item.id !== audioId) {
  186. item.pause();
  187. }
  188. } else {
  189. item.pause();
  190. }
  191. });
  192. let video = document.getElementsByTagName("video");
  193. video.forEach((vItem) => {
  194. vItem.pause();
  195. });
  196. if (this.audio.playing) {
  197. this.$refs[audioId].pause();
  198. this.audio.playing = false;
  199. this.$emit("handleListenRead", false);
  200. this.isClick = false;
  201. } else {
  202. if (this.count == 0) {
  203. this.audio.loading = true;
  204. this.count++;
  205. }
  206. this.$refs[audioId].play();
  207. this.$emit("handleChangeStopAudio");
  208. this.$emit("handleListenRead", true);
  209. this.isClick = true;
  210. }
  211. },
  212. oncanplaythrough() {
  213. let _this = this;
  214. // setTimeout(() => {
  215. _this.audio.loading = false;
  216. // }, 10000);
  217. },
  218. // 点击 拖拽播放音频
  219. changeCurrentTime(value) {
  220. let audioId = this.audioId;
  221. this.$refs[audioId].play();
  222. this.audio.playing = true;
  223. this.$refs[audioId].currentTime = parseInt(
  224. (value / 100) * this.audio.maxTime
  225. );
  226. },
  227. mousedown() {
  228. let audioId = this.audioId;
  229. this.$refs[audioId].pause();
  230. this.audio.playing = false;
  231. },
  232. // 进度条格式化toolTip
  233. formatProcessToolTip(index) {
  234. index = parseInt((this.audio.maxTime / 100) * index);
  235. return this.realFormatSecond(index);
  236. },
  237. // 音频加载完之后
  238. onLoadedmetadata(res) {
  239. console.log(res.target.duration);
  240. this.audio.maxTime = parseInt(res.target.duration);
  241. this.audioAllTime = this.realFormatSecond(this.audio.maxTime);
  242. },
  243. // 当音频当前时间改变后,进度条也要改变
  244. onTimeupdate(res) {
  245. let audioId = this.audioId;
  246. this.audio.currentTime = res.target.currentTime;
  247. if(this.getCurTime){
  248. this.getCurTime(res.target.currentTime);
  249. }
  250. if(this.isRepeat){
  251. if(this.audio.currentTime>=this.audio.maxTime || this.audio.currentTime<=0){
  252. this.onTimeupdateTime(0,true)
  253. }
  254. }
  255. this.playValue = (this.audio.currentTime / this.audio.maxTime) * 100;
  256. },
  257. onTimeupdateTime(res, playFlag) {
  258. if (!res&&res!=0) return;
  259. let audioId = this.audioId;
  260. this.$refs[audioId].currentTime = res;
  261. this.playValue = (res / this.audio.maxTime) * 100;
  262. if (playFlag) {
  263. let audio = document.getElementsByTagName("audio");
  264. audio.forEach((item) => {
  265. if (item.id !== audioId) {
  266. item.pause();
  267. }
  268. });
  269. this.$refs[audioId].play();
  270. }
  271. },
  272. // 将整数转换成 时:分:秒的格式
  273. realFormatSecond(value) {
  274. let theTime = parseInt(value); // 秒
  275. let theTime1 = 0; // 分
  276. let theTime2 = 0; // 小时
  277. if (theTime > 60) {
  278. theTime1 = parseInt(theTime / 60);
  279. theTime = parseInt(theTime % 60);
  280. if (theTime1 > 60) {
  281. theTime2 = parseInt(theTime1 / 60);
  282. theTime1 = parseInt(theTime1 % 60);
  283. }
  284. }
  285. let result = String(parseInt(theTime));
  286. if (result < 10) {
  287. result = "0" + result;
  288. }
  289. if (theTime1 > 0) {
  290. result = String(parseInt(theTime1)) + ":" + result;
  291. if (theTime1 < 10) {
  292. result = "0" + result;
  293. }
  294. } else {
  295. result = "00:" + result;
  296. }
  297. if (theTime2 > 0) {
  298. result = String(parseInt(theTime2)) + ":" + result;
  299. if (theTime2 < 10) {
  300. result = "0" + result;
  301. }
  302. } else {
  303. // result = "00:" + result;
  304. }
  305. return result;
  306. },
  307. // 倍速按钮
  308. handleSpeed(val) {
  309. let audio = this.$refs[this.audioId];
  310. audio.playbackRate = val * 0.5;
  311. this.playbackRateValue = val * 0.5 + 'x'
  312. },
  313. // 改变音量
  314. changeSound(val) {
  315. let audio = this.$refs[this.audioId];
  316. audio.volume = val / 100;
  317. },
  318. changeRepeat() {
  319. let _this = this;
  320. _this.isRepeat = !_this.isRepeat;
  321. },
  322. },
  323. // 生命周期-创建之前
  324. beforeCreated() {},
  325. // 生命周期-更新之前
  326. beforUpdate() {},
  327. // 生命周期-销毁之前
  328. beforeDestory() {},
  329. // 生命周期-销毁完成
  330. destoryed() {},
  331. };
  332. </script>
  333. <style lang="scss" scoped>
  334. /* @import url(); 引入css类 */
  335. .Audio {
  336. width: 100%;
  337. .audioLine {
  338. display: flex;
  339. align-items: center;
  340. width: 100%;
  341. .play {
  342. cursor: pointer;
  343. a{
  344. width: 32px;
  345. height: 32px;
  346. display: block;
  347. text-align: center;
  348. background: #EEF3FF;
  349. border-radius: 4px;
  350. // &:hover,&.active{
  351. // background: #EEF3FF;
  352. // border-radius: 4px;
  353. // }
  354. }
  355. .el-icon-loading,.icon-svg{
  356. color: #175DFF;
  357. width: 24px;
  358. height: 24px;
  359. margin-top: 4px;
  360. }
  361. }
  362. .time-box {
  363. font-size: 16px;
  364. line-height: 24px;
  365. color:#2F3742;
  366. margin-right: 10px;
  367. min-width: 66px;
  368. text-align: right;
  369. }
  370. }
  371. .icon-voice,.icon-repeat{
  372. width: 24px;
  373. height: 24px;
  374. color: #175DFF;
  375. cursor: pointer;
  376. flex-shrink: 0;
  377. }
  378. .icon-repeat{
  379. margin: 0 8px;
  380. color: #D0D3D9;
  381. &.active{
  382. color: #175DFF;
  383. }
  384. }
  385. }
  386. </style>
  387. <style lang="scss">
  388. .Audio {
  389. .el-slider{
  390. height: 20px !important;
  391. margin-right: 8px;
  392. }
  393. .el-slider__bar {
  394. height: 20px;
  395. background: #175DFF;
  396. border-radius: 20px;
  397. }
  398. .el-slider__button {
  399. background: #175DFF;
  400. border: none;
  401. }
  402. .el-slider__button-wrapper {
  403. width: 25px;
  404. }
  405. .el-slider__button-wrapper {
  406. position: relative;
  407. z-index: 0;
  408. }
  409. .el-slider__button {
  410. width: 16px;
  411. height: 16px;
  412. top: 17px;
  413. position: absolute;
  414. opacity: 0;
  415. }
  416. .el-slider__runway {
  417. margin: 0;
  418. padding: 0;
  419. background: #e5e5e5;
  420. border-radius: 20px;
  421. height: 20px;
  422. }
  423. .el-slider {
  424. position: relative;
  425. }
  426. .el-dropdown{
  427. margin: 0 8px;
  428. }
  429. }
  430. </style>