index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <template>
  2. <view class="container global-audio-box" v-if="showAudioPop">
  3. <view class="bg-overlay" @click="isShowMaskHandler"></view>
  4. <view class="audio-box">
  5. <view class="activity-title">
  6. {{ activityTitle }}
  7. <view class="icon-cross" @click.stop="isShowMaskHandler">
  8. <van-icon name="cross" font-size="32" />
  9. </view>
  10. </view>
  11. <view class="audio-card">
  12. <view class="slider-paly">
  13. <image @click="speedReverseHandler('reverse')" class="speed-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/fastReverse_icon.png"></image>
  14. <view style="flex: 1; margin: 0 30rpx 0 38rpx; padding-top: 20rpx">
  15. <slider
  16. activeColor="#376cbb"
  17. :max="audioTime"
  18. :value="curTime"
  19. @touchstart="touchstartHandler"
  20. @change="handleAudioSliderChange($event)"
  21. @changing="handleAudioSliderChangeing($event)"
  22. block-size="16"
  23. class="slider"
  24. />
  25. <view class="card-time">
  26. <text class="time">{{ curTime | formatVoiceTime }}</text>
  27. <text class="time">{{ audioTime | formatVoiceTime }}</text>
  28. </view>
  29. </view>
  30. <image @click="speedReverseHandler('speed')" class="speed-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/speed_icon.png"></image>
  31. </view>
  32. <view class="fast-reverse">
  33. <block v-for="(item, index) in timesTheSpeed" :key="item.value">
  34. <view class="speed-button multiple-button global_content_center" v-if="isTimes == item.value" @click="isTimesHandler(index)">
  35. {{ item.name }}
  36. </view>
  37. </block>
  38. <view>
  39. <image v-if="PreviousNextSong == 0" class="previous_play_next" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/previous_disable.png"></image>
  40. <image
  41. v-else
  42. @click="isPreviousNextSongHandler(PreviousNextSong == 0, '上')"
  43. class="previous_play_next"
  44. src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/previous_play.png"
  45. ></image>
  46. </view>
  47. <view class="is-paly-card" @click="handleChangePlayStatus">
  48. <image v-if="play" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/suspend_play.png"></image>
  49. <image v-else src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/play_circle_filled.png"></image>
  50. </view>
  51. <view>
  52. <image v-if="PreviousNextSong == playlistData.length - 1" class="previous_play_next" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/next_disable.png"></image>
  53. <image
  54. @click="isPreviousNextSongHandler(PreviousNextSong == playlistData.length - 1, '下')"
  55. v-else
  56. class="previous_play_next"
  57. src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/next_play.png"
  58. ></image>
  59. </view>
  60. <view class="speed-button global_content_center play-list-content" @click="showTabulation = true">
  61. <van-icon name="bars" size="28" />
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. <van-popup :show="showTabulation" closeable position="bottom" round custom-style="height: 50%;" @close="onCloseHandler">
  67. <view class="play-ul">
  68. <view class="global_title play-ul_title">播放列表</view>
  69. <view class="play-li-box">
  70. <view class="play-li" v-for="(item, index) in playlistData" :key="item.indexId" @click="palyHandler(item)">
  71. <view class="item-content-play-list">
  72. <view class="text_oneLine play-li-name">{{ item.Name }}</view>
  73. <image v-if="audioInit.indexId == item.indexId" class="play_audio" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/play_audio.png"></image>
  74. </view>
  75. <view class="delete_play">
  76. <image @click.native.stop="removeAudioListHandler(item, index)" class="delete_play" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/delete_play.png"></image>
  77. </view>
  78. </view>
  79. </view>
  80. </view>
  81. </van-popup>
  82. </view>
  83. </template>
  84. <script>
  85. import { activity, Reports } from "@/config/api";
  86. export default {
  87. name: "",
  88. filters: {
  89. formatVoiceTime(e) {
  90. let m = parseInt(e / 60);
  91. let s = parseInt(e % 60);
  92. return `${m > 9 ? m : "0" + m}:${s > 9 ? s : "0" + s}`;
  93. },
  94. },
  95. components: {},
  96. props: {
  97. showAudioPop: {
  98. type: Boolean,
  99. default: false,
  100. required: true,
  101. },
  102. },
  103. data() {
  104. return {
  105. curTime: 0,
  106. audioTime: 0, //当前音频总时长
  107. title: "", //当前音频标题
  108. activityTitle: "", //当前活动标题
  109. play: false,
  110. isosName: "",
  111. palyTimeout: null,
  112. palyTime: 0,
  113. timesTheSpeed: [
  114. { name: "倍速", value: 1 },
  115. { name: "1.25", value: 1.25 },
  116. { name: "1.5", value: 1.5 },
  117. { name: "2", value: 2 },
  118. ],
  119. isRecord: true, //是否记录播放
  120. showTabulation: false,
  121. };
  122. },
  123. computed: {
  124. //重新
  125. audioInit() {
  126. return {
  127. activityId: this.$store.state.audioBg.activityId,
  128. reportId: this.$store.state.audioBg.reportId,
  129. indexId: this.$store.state.audioBg.indexId,
  130. };
  131. },
  132. // 几倍的播放速度
  133. isTimes() {
  134. return this.$store.state.audioBg.multiple;
  135. },
  136. //进度条是否在滑动
  137. isSlide() {
  138. return this.$store.state.audioBg.isDragSlide;
  139. },
  140. isEnded() {
  141. return this.$store.state.audioBg.isAudioEnded;
  142. },
  143. // 播放列表数据
  144. playlistData() {
  145. return this.$store.state.audioJoinPlaylist.playlist;
  146. },
  147. // 上、下一首歌曲禁用
  148. PreviousNextSong() {
  149. const curAudio = this.$store.state.audioBg.list;
  150. let indexId = this.audioInit.indexId;
  151. let indexPlay = this.playlistData.findIndex((_) => _.Name === curAudio.Name && _.indexId === indexId);
  152. return indexPlay;
  153. },
  154. },
  155. watch: {
  156. audioInit: {
  157. // 切换了音频播放 重置下数据
  158. handler(nval) {
  159. this.init();
  160. },
  161. immediate: true,
  162. },
  163. },
  164. created() {},
  165. mounted() {
  166. uni.getSystemInfo({
  167. //判断机型
  168. success: (res) => {
  169. this.isosName = res.osName;
  170. },
  171. });
  172. },
  173. methods: {
  174. //点击隐藏事件
  175. isShowMaskHandler() {
  176. this.$emit("update:showAudioPop", false);
  177. },
  178. //数据初次加载
  179. init() {
  180. const curAudio = this.$store.state.audioBg.list;
  181. this.isExistList(curAudio);
  182. if ((this.globalBgAudioManager.src != curAudio.Url && curAudio.Url) || (this.isEnded && this.isosName !== "ios" && curAudio.Url)) {
  183. this.$store.commit("audioBg/setAudioEnd", false);
  184. this.globalBgAudioManager.playbackRate = 1;
  185. this.$store.commit("audioBg/setMultiple", 1);
  186. this.$store.commit("audioBg/updateAudioTime", 0);
  187. this.globalBgAudioManager.src = curAudio.Url;
  188. this.globalBgAudioManager.title = curAudio.Name;
  189. this.globalBgAudioManager.startTime = 0;
  190. this.curTime = 0;
  191. } else {
  192. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  193. }
  194. this.audioTime = curAudio.PlaySeconds;
  195. this.title = curAudio.Name ? curAudio.Name.replace(/<\/?font.*?>/g, "") : "";
  196. let activityTitle = this.$store.state.audioBg.activityTitle;
  197. this.activityTitle = activityTitle ? activityTitle.replace(/<\/?font.*?>/g, "") : "";
  198. this.play = !this.globalBgAudioManager.paused;
  199. this.listenAudio();
  200. },
  201. //音频播放事件
  202. listenAudio() {
  203. // 音频播放
  204. this.globalBgAudioManager.onPlay(async () => {
  205. if (this.isRecord) {
  206. this.palyTime = 0;
  207. this.backAudioPlay();
  208. this.palyTimeout = setInterval(() => {
  209. this.palyTime++;
  210. }, 1000);
  211. }
  212. this.play = true;
  213. this.$store.commit("audioBg/updateAudioPause", false);
  214. this.isRecord = true;
  215. });
  216. // 音频暂停
  217. this.globalBgAudioManager.onPause(() => {
  218. this.backAudioPlay();
  219. clearInterval(this.palyTimeout);
  220. this.play = false;
  221. this.$store.commit("audioBg/updateAudioPause", true);
  222. });
  223. // 音频停止
  224. this.globalBgAudioManager.onStop(() => {
  225. this.backAudioPlay();
  226. clearInterval(this.palyTimeout);
  227. this.$emit("update:showAudioPop", false);
  228. if (this.isosName == "ios") {
  229. this.play = false;
  230. this.$store.commit("audioBg/removeAudio");
  231. } else {
  232. this.play = false;
  233. this.$store.commit("audioBg/updateAudioPause", true);
  234. }
  235. });
  236. // 音频onEnded
  237. this.globalBgAudioManager.onEnded(() => {
  238. const curAudio = this.$store.state.audioBg.list;
  239. let indexId = this.audioInit.indexId;
  240. this.onEndedpalyHandler(curAudio, indexId);
  241. this.backAudioPlay();
  242. clearInterval(this.palyTimeout);
  243. });
  244. // 音频onError
  245. this.globalBgAudioManager.onError((e) => {
  246. this.$store.commit("audioBg/removeAudio");
  247. this.$store.commit("audioBg/setMultiple", 1);
  248. uni.showToast({
  249. title: "音频播放错误",
  250. icon: "none",
  251. });
  252. });
  253. // 音频的播放时间更新
  254. this.globalBgAudioManager.onTimeUpdate(() => {
  255. if (this.globalBgAudioManager.src && parseInt(this.globalBgAudioManager.currentTime) !== 0 && !this.isSlide) {
  256. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  257. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  258. }
  259. });
  260. },
  261. // 拖动进度条
  262. handleAudioSliderChangeing(e) {
  263. this.curTime = e.detail.value;
  264. },
  265. // 拖动进度条
  266. handleAudioSliderChange(e) {
  267. const value = e.detail.value;
  268. this.globalBgAudioManager.seek(value);
  269. this.isRecord = false;
  270. setTimeout(() => {
  271. this.$store.commit("audioBg/setSlide", false);
  272. }, 300);
  273. },
  274. // 音频点击暂停播放
  275. handleChangePlayStatus() {
  276. if (!this.globalBgAudioManager.paused) {
  277. this.globalBgAudioManager.pause();
  278. } else {
  279. this.globalBgAudioManager.play();
  280. }
  281. },
  282. // 倍速播放
  283. isTimesHandler(i) {
  284. let index = i == 3 ? 0 : i + 1;
  285. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  286. this.globalBgAudioManager.playbackRate = this.isTimes;
  287. this.globalBgAudioManager.startTime = this.curTime;
  288. if (this.globalBgAudioManager.paused) {
  289. this.globalBgAudioManager.play();
  290. }
  291. this.isRecord = false;
  292. },
  293. //快进 快退
  294. speedReverseHandler(type) {
  295. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  296. isTime = isTime <= 0 ? 0 : isTime >= this.audioTime ? this.audioTime - 1 : isTime;
  297. this.globalBgAudioManager.seek(isTime);
  298. this.isRecord = false;
  299. },
  300. // 播放了记录
  301. backAudioPlay() {
  302. let recordList = this.$store.state.audioBg.recordList;
  303. if ((this.$store.state.audioBg.activityId || this.audioInit.indexId) && this.palyTime >= 0) {
  304. activity.backAudioPlay({
  305. SourceId: recordList.SourceId || recordList.ActivityId,
  306. SourceType: recordList.Type ? recordList.Type : 1,
  307. PlaySeconds: this.palyTime,
  308. PageRouter: this.$store.state.pageRouterReport,
  309. });
  310. } else if (this.$store.state.audioBg.reportId && this.palyTime >= 0) {
  311. Reports.reportVoiceHistoryAdd({
  312. ArticleId: this.$store.state.audioBg.reportId,
  313. PlaySeconds: this.palyTime,
  314. PageRouter: this.$store.state.pageRouterReport,
  315. });
  316. }
  317. },
  318. // 手指离开了拖动进度条
  319. touchstartHandler() {
  320. this.$store.commit("audioBg/setSlide", true);
  321. },
  322. // 关闭播放列表
  323. onCloseHandler() {
  324. this.showTabulation = false;
  325. },
  326. // 点击了播放列表
  327. palyHandler(item) {
  328. if (this.audioInit.indexId == item.indexId) {
  329. if (this.globalBgAudioManager.paused) {
  330. this.globalBgAudioManager.play();
  331. } else {
  332. this.globalBgAudioManager.pause();
  333. }
  334. } else {
  335. this.playlistDataInit(item);
  336. }
  337. },
  338. // 播放结束后 播放下一首
  339. onEndedpalyHandler(curAudio, indexId) {
  340. let item = this.playlistData[this.PreviousNextSong + 1];
  341. if (item) {
  342. this.playlistDataInit(item);
  343. } else {
  344. this.$emit("update:showAudioPop", false);
  345. this.$store.commit("audioBg/setAudioEnd", true);
  346. this.$store.commit("audioBg/parseIntAudio", false);
  347. this.$store.commit("audioBg/removeAudio");
  348. }
  349. },
  350. // 上一首 下一首
  351. isPreviousNextSongHandler(isPlay, type) {
  352. if (isPlay) return;
  353. let item = this.playlistData[type == "上" ? this.PreviousNextSong - 1 : this.PreviousNextSong + 1];
  354. this.playlistDataInit(item);
  355. },
  356. // 处理播放列表的数据
  357. playlistDataInit(item) {
  358. if (item) {
  359. let VoiceList = {
  360. Url: item.Url,
  361. Name: item.Name,
  362. PlaySeconds: item.PlaySeconds,
  363. };
  364. this.$store.commit("audioBg/addAudio", { list: VoiceList, indexId: item.indexId, activityTitle: item.activityTitle, recordList: item.recordList });
  365. this.init();
  366. }
  367. },
  368. // 数据是否存在
  369. isExistList(item) {
  370. const playlist = this.playlistData.some((key) => key.indexId === this.audioInit.indexId);
  371. if (!playlist && item.Url) {
  372. let dataList = {
  373. Url: item.Url,
  374. Name: item.Name,
  375. PlaySeconds: item.PlaySeconds,
  376. indexId: this.audioInit.indexId,
  377. activityTitle: item.Name,
  378. recordList: this.$store.state.audioBg.recordList,
  379. };
  380. this.$store.commit("audioJoinPlaylist/addUnshiftPlaylist", { dataList });
  381. }
  382. },
  383. // 点击删除列表
  384. removeAudioListHandler(item, index) {
  385. let list = this.playlistData.filter((_) => _.indexId != item.indexId);
  386. this.$store.commit("audioJoinPlaylist/removeAudio", { list });
  387. if (index === this.playlistData.length) {
  388. this.globalBgAudioManager.stop();
  389. } else {
  390. this.playlistDataInit(this.playlistData[index]);
  391. }
  392. },
  393. },
  394. };
  395. </script>
  396. <style scoped lang="scss">
  397. .global-audio-box {
  398. display: flex;
  399. height: 100%;
  400. position: fixed;
  401. width: 100%;
  402. top: 0;
  403. left: 0;
  404. z-index: 111;
  405. .bg-overlay {
  406. width: 100%;
  407. height: 100%;
  408. position: absolute;
  409. top: 0;
  410. left: 0;
  411. background-color: rgba(0, 0, 0, 0.7);
  412. }
  413. .activity-title {
  414. position: relative;
  415. width: 100%;
  416. font-size: 30rpx;
  417. font-weight: 500;
  418. margin-bottom: 35rpx;
  419. padding-right: 30rpx;
  420. .icon-cross {
  421. position: absolute;
  422. right: 0;
  423. top: 50%;
  424. transform: translateY(-50%);
  425. padding: 10rpx;
  426. }
  427. }
  428. .audio-box {
  429. position: absolute;
  430. bottom: 0;
  431. left: 0;
  432. padding: 30rpx;
  433. padding-bottom: constant(safe-area-inset-bottom);
  434. padding-bottom: env(safe-area-inset-bottom);
  435. width: 100%;
  436. background: #ffffff;
  437. box-sizing: border-box;
  438. border-radius: 30rpx 30rpx 0 0;
  439. }
  440. .audio-card {
  441. width: 100%;
  442. height: 282rpx;
  443. background: #f9f9f9;
  444. border-radius: 16rpx;
  445. margin: 0 auto;
  446. padding: 30rpx 0;
  447. .slider {
  448. width: 100%;
  449. margin: 0;
  450. }
  451. .slider-paly {
  452. display: flex;
  453. height: 80rpx;
  454. align-items: center;
  455. }
  456. .card-title {
  457. color: #376cbb;
  458. font-size: 28rpx;
  459. padding: 0 40rpx;
  460. text-align: center;
  461. margin-bottom: 35rpx;
  462. }
  463. .card-time {
  464. display: flex;
  465. justify-content: space-between;
  466. color: #999999;
  467. font-size: 20rpx;
  468. }
  469. .is-paly-card {
  470. width: 150rpx;
  471. height: 150rpx;
  472. flex-shrink: 0;
  473. image {
  474. width: 150rpx;
  475. height: 150rpx;
  476. }
  477. }
  478. .fast-reverse {
  479. display: flex;
  480. align-items: center;
  481. justify-content: space-around;
  482. margin-top: 30rpx;
  483. .speed-button {
  484. width: 88rpx;
  485. height: 58rpx;
  486. background: #e5efff;
  487. border-radius: 8rpx;
  488. color: $uni-color-new;
  489. font-size: 36rpx;
  490. }
  491. .multiple-button {
  492. font-size: 30rpx;
  493. }
  494. .play-list-content {
  495. width: 64rpx;
  496. }
  497. }
  498. }
  499. .speed-img {
  500. width: 50rpx;
  501. height: 50rpx;
  502. }
  503. .play-ul {
  504. padding: 30rpx 0;
  505. .play-ul_title {
  506. padding-left: 30rpx;
  507. }
  508. .play-li-box {
  509. margin-top: 35rpx;
  510. border-top: 1rpx solid #dcdfe6;
  511. }
  512. .play-li {
  513. display: flex;
  514. align-items: center;
  515. justify-content: space-between;
  516. height: 92rpx;
  517. padding: 0 30rpx;
  518. border-bottom: 1rpx solid #dcdfe6;
  519. .delete_play {
  520. flex-shrink: 0;
  521. width: 36rpx;
  522. height: 36rpx;
  523. margin-left: 5rpx;
  524. image {
  525. width: 36rpx;
  526. height: 36rpx;
  527. }
  528. }
  529. .item-content-play-list {
  530. width: 600rpx;
  531. display: flex;
  532. align-items: center;
  533. flex-shrink: 0;
  534. .play-li-name {
  535. display: inline-block;
  536. }
  537. .play_audio {
  538. display: inline-block;
  539. width: 36rpx;
  540. height: 36rpx;
  541. margin-left: 2rpx;
  542. }
  543. }
  544. }
  545. }
  546. .previous_play_next {
  547. width: 100rpx;
  548. height: 100rpx;
  549. }
  550. }
  551. </style>