index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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="playImg" 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. <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/paly_list_play.png"></image>
  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', audioInit.indexId == item.indexId && 'active-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.gif"></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. playImg: 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. isCommunication: false,
  122. };
  123. },
  124. computed: {
  125. //重新
  126. audioInit() {
  127. return {
  128. activityId: this.$store.state.audioBg.activityId,
  129. reportId: this.$store.state.audioBg.reportId,
  130. indexId: this.$store.state.audioBg.indexId,
  131. };
  132. },
  133. // 几倍的播放速度
  134. isTimes() {
  135. return this.$store.state.audioBg.multiple;
  136. },
  137. //进度条是否在滑动
  138. isSlide() {
  139. return this.$store.state.audioBg.isDragSlide;
  140. },
  141. isEnded() {
  142. return this.$store.state.audioBg.isAudioEnded;
  143. },
  144. // 播放列表数据
  145. playlistData() {
  146. return this.$store.state.audioJoinPlaylist.playlist;
  147. },
  148. // 上、下一首歌曲禁用
  149. PreviousNextSong() {
  150. const curAudio = this.$store.state.audioBg.list;
  151. let indexId = this.audioInit.indexId;
  152. let indexPlay = this.playlistData.findIndex((_) => _.Name === curAudio.Name && _.indexId === indexId);
  153. return indexPlay;
  154. },
  155. },
  156. watch: {
  157. audioInit: {
  158. // 切换了音频播放 重置下数据
  159. handler(nval) {
  160. this.init();
  161. },
  162. immediate: true,
  163. },
  164. },
  165. created() {},
  166. mounted() {
  167. uni.getSystemInfo({
  168. //判断机型
  169. success: (res) => {
  170. this.isosName = res.osName;
  171. },
  172. });
  173. uni.$on("play-list-emit", () => {
  174. this.showTabulation = true;
  175. this.isCommunication = true;
  176. });
  177. },
  178. methods: {
  179. //点击隐藏事件
  180. isShowMaskHandler() {
  181. this.$emit("update:showAudioPop", false);
  182. },
  183. //数据初次加载
  184. init() {
  185. const curAudio = this.$store.state.audioBg.list;
  186. this.isExistList(curAudio);
  187. if ((this.globalBgAudioManager.src != curAudio.Url && curAudio.Url) || (this.isEnded && this.isosName !== "ios" && curAudio.Url)) {
  188. this.$store.commit("audioBg/setAudioEnd", false);
  189. this.globalBgAudioManager.playbackRate = 1;
  190. this.$store.commit("audioBg/setMultiple", 1);
  191. this.$store.commit("audioBg/updateAudioTime", 0);
  192. this.globalBgAudioManager.src = curAudio.Url;
  193. this.globalBgAudioManager.title = curAudio.Name;
  194. this.globalBgAudioManager.startTime = 0;
  195. this.curTime = 0;
  196. } else {
  197. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  198. }
  199. this.audioTime = curAudio.PlaySeconds;
  200. this.title = curAudio.Name ? curAudio.Name.replace(/<\/?font.*?>/g, "") : "";
  201. let activityTitle = this.$store.state.audioBg.activityTitle;
  202. this.activityTitle = activityTitle ? activityTitle.replace(/<\/?font.*?>/g, "") : "";
  203. this.playImg = !this.globalBgAudioManager.paused;
  204. this.listenAudio();
  205. },
  206. //音频播放事件
  207. listenAudio() {
  208. // 音频播放
  209. this.globalBgAudioManager.onPlay(async () => {
  210. if (this.isRecord) {
  211. this.palyTime = 0;
  212. this.backAudioPlay();
  213. this.palyTimeout = setInterval(() => {
  214. this.palyTime++;
  215. }, 1000);
  216. }
  217. this.playImg = true;
  218. this.$store.commit("audioBg/updateAudioPause", false);
  219. this.isRecord = true;
  220. });
  221. // 音频暂停
  222. this.globalBgAudioManager.onPause(() => {
  223. this.backAudioPlay();
  224. clearInterval(this.palyTimeout);
  225. this.playImg = false;
  226. this.$store.commit("audioBg/updateAudioPause", true);
  227. });
  228. // 音频停止
  229. this.globalBgAudioManager.onStop(() => {
  230. this.showTabulation = false;
  231. this.backAudioPlay();
  232. clearInterval(this.palyTimeout);
  233. this.$emit("update:showAudioPop", false);
  234. this.playImg = false;
  235. this.$store.commit("audioBg/removeAudio");
  236. if (this.isosName !== "ios") {
  237. } else {
  238. this.$store.commit("audioBg/updateAudioPause", true);
  239. }
  240. });
  241. // 音频onEnded
  242. this.globalBgAudioManager.onEnded(() => {
  243. const curAudio = this.$store.state.audioBg.list;
  244. let indexId = this.audioInit.indexId;
  245. this.onEndedpalyHandler(curAudio, indexId);
  246. this.backAudioPlay();
  247. clearInterval(this.palyTimeout);
  248. });
  249. // 音频onError
  250. this.globalBgAudioManager.onError((e) => {
  251. this.$store.commit("audioBg/removeAudio");
  252. this.$store.commit("audioBg/setMultiple", 1);
  253. uni.showToast({
  254. title: "音频播放错误",
  255. icon: "none",
  256. });
  257. });
  258. // 音频的播放时间更新
  259. this.globalBgAudioManager.onTimeUpdate(() => {
  260. if (this.globalBgAudioManager.src && parseInt(this.globalBgAudioManager.currentTime) !== 0 && !this.isSlide) {
  261. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  262. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  263. }
  264. });
  265. },
  266. // 拖动进度条
  267. handleAudioSliderChangeing(e) {
  268. this.curTime = e.detail.value;
  269. },
  270. // 拖动进度条
  271. handleAudioSliderChange(e) {
  272. const value = e.detail.value;
  273. this.globalBgAudioManager.seek(value);
  274. this.isRecord = false;
  275. setTimeout(() => {
  276. this.$store.commit("audioBg/setSlide", false);
  277. }, 300);
  278. },
  279. // 音频点击暂停播放
  280. handleChangePlayStatus() {
  281. if (!this.globalBgAudioManager.paused) {
  282. this.globalBgAudioManager.pause();
  283. } else {
  284. this.globalBgAudioManager.play();
  285. }
  286. },
  287. // 倍速播放
  288. isTimesHandler(i) {
  289. let index = i == 3 ? 0 : i + 1;
  290. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  291. this.globalBgAudioManager.playbackRate = this.isTimes;
  292. this.globalBgAudioManager.startTime = this.curTime;
  293. if (this.globalBgAudioManager.paused) {
  294. this.globalBgAudioManager.play();
  295. }
  296. this.isRecord = false;
  297. },
  298. //快进 快退
  299. speedReverseHandler(type) {
  300. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  301. isTime = isTime <= 0 ? 0 : isTime >= this.audioTime ? this.audioTime - 1 : isTime;
  302. this.globalBgAudioManager.seek(isTime);
  303. this.isRecord = false;
  304. },
  305. // 播放了记录
  306. backAudioPlay() {
  307. let recordList = this.$store.state.audioBg.recordList;
  308. if ((this.$store.state.audioBg.activityId || this.audioInit.indexId) && this.palyTime >= 0) {
  309. activity.backAudioPlay({
  310. SourceId: recordList.SourceId || recordList.ActivityId,
  311. SourceType: recordList.Type ? recordList.Type : 1,
  312. PlaySeconds: this.palyTime,
  313. PageRouter: this.$store.state.pageRouterReport,
  314. });
  315. } else if (this.$store.state.audioBg.reportId && this.palyTime >= 0) {
  316. Reports.reportVoiceHistoryAdd({
  317. ArticleId: this.$store.state.audioBg.reportId,
  318. PlaySeconds: this.palyTime,
  319. PageRouter: this.$store.state.pageRouterReport,
  320. });
  321. }
  322. },
  323. // 手指离开了拖动进度条
  324. touchstartHandler() {
  325. this.$store.commit("audioBg/setSlide", true);
  326. },
  327. // 关闭播放列表
  328. onCloseHandler() {
  329. this.showTabulation = false;
  330. if (this.isCommunication) {
  331. this.isCommunication = false;
  332. this.isShowMaskHandler();
  333. }
  334. },
  335. // 点击了播放列表
  336. palyHandler(item) {
  337. console.log(item);
  338. if (this.audioInit.indexId == item.indexId) {
  339. } else {
  340. this.playlistDataInit(item);
  341. }
  342. },
  343. // 播放结束后 播放下一首
  344. onEndedpalyHandler(curAudio, indexId) {
  345. let item = this.playlistData[this.PreviousNextSong + 1];
  346. if (item) {
  347. this.playlistDataInit(item);
  348. } else {
  349. this.$emit("update:showAudioPop", false);
  350. this.$store.commit("audioBg/setAudioEnd", true);
  351. this.$store.commit("audioBg/parseIntAudio", false);
  352. this.$store.commit("audioBg/removeAudio");
  353. }
  354. },
  355. // 上一首 下一首
  356. isPreviousNextSongHandler(isPlay, type) {
  357. if (isPlay) return;
  358. let item = this.playlistData[type == "上" ? this.PreviousNextSong - 1 : this.PreviousNextSong + 1];
  359. this.playlistDataInit(item);
  360. },
  361. // 处理播放列表的数据
  362. playlistDataInit(item) {
  363. if (item) {
  364. let VoiceList = {
  365. Url: item.Url,
  366. Name: item.Name,
  367. PlaySeconds: item.PlaySeconds,
  368. };
  369. this.$store.commit("audioBg/addAudio", { list: VoiceList, indexId: item.indexId, activityTitle: item.activityTitle, recordList: item.recordList });
  370. this.init();
  371. }
  372. },
  373. // 数据是否存在
  374. isExistList(item) {
  375. const playlist = this.playlistData.some((key) => key.indexId === this.audioInit.indexId);
  376. if (!playlist && item.Url) {
  377. let dataList = {
  378. Url: item.Url,
  379. Name: item.Name,
  380. PlaySeconds: item.PlaySeconds,
  381. indexId: this.audioInit.indexId,
  382. activityTitle: item.Name,
  383. recordList: this.$store.state.audioBg.recordList,
  384. };
  385. this.$store.commit("audioJoinPlaylist/addUnshiftPlaylist", { dataList });
  386. }
  387. },
  388. // 点击删除列表
  389. removeAudioListHandler(item, index) {
  390. const indexKey = this.playlistData.findIndex((key) => key.indexId === this.audioInit.indexId);
  391. let list = this.playlistData.filter((_) => _.indexId != item.indexId);
  392. this.$store.commit("audioJoinPlaylist/removeAudio", { list });
  393. if (index === this.playlistData.length && indexKey === index) {
  394. this.globalBgAudioManager.stop();
  395. } else if (indexKey < index) {
  396. } else if (indexKey === index) {
  397. this.playlistDataInit(this.playlistData[index]);
  398. }
  399. },
  400. },
  401. };
  402. </script>
  403. <style scoped lang="scss">
  404. .global-audio-box {
  405. display: flex;
  406. height: 100%;
  407. position: fixed;
  408. width: 100%;
  409. top: 0;
  410. left: 0;
  411. z-index: 111;
  412. .bg-overlay {
  413. width: 100%;
  414. height: 100%;
  415. position: absolute;
  416. top: 0;
  417. left: 0;
  418. background-color: rgba(0, 0, 0, 0.7);
  419. }
  420. .activity-title {
  421. position: relative;
  422. width: 100%;
  423. font-size: 30rpx;
  424. font-weight: 500;
  425. margin-bottom: 35rpx;
  426. padding-right: 50rpx;
  427. .icon-cross {
  428. position: absolute;
  429. right: 0;
  430. top: 50%;
  431. transform: translateY(-50%);
  432. padding: 10rpx;
  433. }
  434. }
  435. .audio-box {
  436. position: absolute;
  437. bottom: 0;
  438. left: 0;
  439. padding: 30rpx;
  440. padding-bottom: constant(safe-area-inset-bottom);
  441. padding-bottom: env(safe-area-inset-bottom);
  442. width: 100%;
  443. background: #ffffff;
  444. box-sizing: border-box;
  445. border-radius: 30rpx 30rpx 0 0;
  446. }
  447. .audio-card {
  448. width: 100%;
  449. height: 282rpx;
  450. background: #f9f9f9;
  451. border-radius: 16rpx;
  452. margin: 0 auto;
  453. padding: 30rpx 0;
  454. .slider {
  455. width: 100%;
  456. margin: 0;
  457. }
  458. .slider-paly {
  459. display: flex;
  460. height: 80rpx;
  461. align-items: center;
  462. }
  463. .card-title {
  464. color: #376cbb;
  465. font-size: 28rpx;
  466. padding: 0 40rpx;
  467. text-align: center;
  468. margin-bottom: 35rpx;
  469. }
  470. .card-time {
  471. display: flex;
  472. justify-content: space-between;
  473. color: #999999;
  474. font-size: 20rpx;
  475. }
  476. .is-paly-card {
  477. width: 150rpx;
  478. height: 150rpx;
  479. flex-shrink: 0;
  480. image {
  481. width: 150rpx;
  482. height: 150rpx;
  483. }
  484. }
  485. .fast-reverse {
  486. display: flex;
  487. align-items: center;
  488. justify-content: space-around;
  489. margin-top: 30rpx;
  490. .speed-button {
  491. width: 88rpx;
  492. height: 58rpx;
  493. background: #e5efff;
  494. border-radius: 8rpx;
  495. color: $uni-color-new;
  496. font-size: 36rpx;
  497. }
  498. .multiple-button {
  499. font-size: 30rpx;
  500. }
  501. .play-list-content {
  502. display: flex;
  503. align-items: center;
  504. justify-content: center;
  505. width: 64rpx;
  506. image {
  507. width: 48rpx;
  508. height: 48rpx;
  509. }
  510. }
  511. }
  512. }
  513. .speed-img {
  514. width: 50rpx;
  515. height: 50rpx;
  516. }
  517. .play-ul {
  518. padding: 30rpx 0;
  519. .play-ul_title {
  520. padding-left: 30rpx;
  521. }
  522. .play-li-box {
  523. height: 880rpx;
  524. overflow: hidden;
  525. margin-top: 35rpx;
  526. border-top: 1rpx solid #dcdfe6;
  527. overflow-y: auto;
  528. }
  529. .play-li {
  530. display: flex;
  531. align-items: center;
  532. justify-content: space-between;
  533. height: 92rpx;
  534. padding: 0 30rpx;
  535. border-bottom: 1rpx solid #dcdfe6;
  536. .delete_play {
  537. flex-shrink: 0;
  538. width: 36rpx;
  539. height: 36rpx;
  540. margin-left: 5rpx;
  541. image {
  542. width: 36rpx;
  543. height: 36rpx;
  544. }
  545. }
  546. .item-content-play-list {
  547. width: 600rpx;
  548. display: flex;
  549. align-items: center;
  550. flex-shrink: 0;
  551. .play-li-name {
  552. display: inline-block;
  553. }
  554. .active-play-li-name {
  555. color: $uni-color-new;
  556. }
  557. .play_audio {
  558. display: inline-block;
  559. width: 36rpx;
  560. height: 36rpx;
  561. margin-left: 2rpx;
  562. flex-shrink: 0;
  563. }
  564. }
  565. }
  566. }
  567. .previous_play_next {
  568. width: 100rpx;
  569. height: 100rpx;
  570. }
  571. }
  572. </style>