index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <template>
  2. <!-- v-if="showAudioPop" -->
  3. <view class="container global-audio-box">
  4. <view class="bg-overlay" @click="isShowMaskHandler"></view>
  5. <view class="audio-box">
  6. <view @click="showTabulation = true"> 最后不要忘了删除 </view>
  7. <view class="activity-title">
  8. {{ activityTitle }}
  9. <view class="icon-cross" @click.stop="isShowMaskHandler">
  10. <van-icon name="cross" font-size="32" />
  11. </view>
  12. </view>
  13. <view class="audio-card">
  14. <view class="card-title text_oneLine" @click="showTabulation = true">
  15. {{ title }}
  16. </view>
  17. <view class="slider-paly">
  18. <view style="flex: 1; padding-top: 20rpx">
  19. <slider
  20. activeColor="#376cbb"
  21. :max="audioTime"
  22. :value="curTime"
  23. @touchstart="touchstartHandler"
  24. @change="handleAudioSliderChange($event)"
  25. @changing="handleAudioSliderChangeing($event)"
  26. block-size="16"
  27. class="slider"
  28. />
  29. <view class="card-time">
  30. <text class="time">{{ curTime | formatVoiceTime }}</text>
  31. <text class="time">{{ audioTime | formatVoiceTime }}</text>
  32. </view>
  33. </view>
  34. <view class="is-paly-card">
  35. <image
  36. @click.stop="handleChangePlayStatus"
  37. :src="play ? 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/play_icon.gif' : 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/pause_icon.png'"
  38. ></image>
  39. </view>
  40. </view>
  41. <view class="fast-reverse">
  42. <image @click="speedReverseHandler('reverse')" class="speed-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/fastReverse_icon.png"></image>
  43. <block v-for="(item, index) in timesTheSpeed" :key="item.value">
  44. <view class="speed-button" v-if="isTimes == item.value" @click="isTimesHandler(index)">
  45. {{ item.name }}
  46. </view>
  47. </block>
  48. <image @click="speedReverseHandler('speed')" class="speed-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/speed_icon.png"></image>
  49. </view>
  50. </view>
  51. </view>
  52. <van-popup :show="showTabulation" closeable position="bottom" round custom-style="height: 50%;" @close="onCloseHandler">
  53. <view>
  54. <view class="global_title">播放列表</view>
  55. <view v-for="item in playlistData" :key="item.indexId" @click="palyHandler(item)">
  56. {{ item.Name }}
  57. </view>
  58. </view>
  59. </van-popup>
  60. </view>
  61. </template>
  62. <script>
  63. import { activity, Reports } from "@/config/api";
  64. export default {
  65. name: "",
  66. filters: {
  67. formatVoiceTime(e) {
  68. let m = parseInt(e / 60);
  69. let s = parseInt(e % 60);
  70. return `${m > 9 ? m : "0" + m}:${s > 9 ? s : "0" + s}`;
  71. },
  72. },
  73. components: {},
  74. props: {
  75. showAudioPop: {
  76. type: Boolean,
  77. default: false,
  78. required: true,
  79. },
  80. },
  81. data() {
  82. return {
  83. curTime: 0,
  84. audioTime: 0, //当前音频总时长
  85. title: "", //当前音频标题
  86. activityTitle: "", //当前活动标题
  87. play: false,
  88. isosName: "",
  89. palyTimeout: null,
  90. palyTime: 0,
  91. timesTheSpeed: [
  92. { name: "倍速", value: 1 },
  93. { name: "1.25倍", value: 1.25 },
  94. { name: "1.5倍", value: 1.5 },
  95. { name: "2倍", value: 2 },
  96. ],
  97. isRecord: true, //是否记录播放
  98. showTabulation: true,
  99. };
  100. },
  101. computed: {
  102. //重新
  103. audioInit() {
  104. return {
  105. activityId: this.$store.state.audioBg.activityId,
  106. reportId: this.$store.state.audioBg.reportId,
  107. indexId: this.$store.state.audioBg.indexId,
  108. };
  109. },
  110. // 几倍的播放速度
  111. isTimes() {
  112. return this.$store.state.audioBg.multiple;
  113. },
  114. //进度条是否在滑动
  115. isSlide() {
  116. return this.$store.state.audioBg.isDragSlide;
  117. },
  118. isEnded() {
  119. return this.$store.state.audioBg.isAudioEnded;
  120. },
  121. playlistData() {
  122. return this.$store.state.audioJoinPlaylist.playlist;
  123. },
  124. },
  125. watch: {
  126. audioInit: {
  127. // 切换了音频播放 重置下数据
  128. handler(nval) {
  129. this.init();
  130. },
  131. immediate: true,
  132. },
  133. },
  134. created() {},
  135. mounted() {
  136. uni.getSystemInfo({
  137. //判断机型
  138. success: (res) => {
  139. this.isosName = res.osName;
  140. },
  141. });
  142. },
  143. methods: {
  144. //点击隐藏事件
  145. isShowMaskHandler() {
  146. this.$emit("update:showAudioPop", false);
  147. },
  148. //数据初次加载
  149. init() {
  150. const curAudio = this.$store.state.audioBg.list;
  151. if ((this.globalBgAudioManager.src != curAudio.Url && curAudio.Url) || (this.isEnded && this.isosName !== "ios" && curAudio.Url)) {
  152. this.$store.commit("audioBg/setAudioEnd", false);
  153. this.globalBgAudioManager.playbackRate = 1;
  154. this.$store.commit("audioBg/setMultiple", 1);
  155. this.$store.commit("audioBg/updateAudioTime", 0);
  156. this.globalBgAudioManager.src = curAudio.Url;
  157. this.globalBgAudioManager.title = curAudio.Name;
  158. this.globalBgAudioManager.startTime = 0;
  159. this.curTime = 0;
  160. } else {
  161. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  162. }
  163. this.audioTime = curAudio.PlaySeconds;
  164. this.title = curAudio.Name ? curAudio.Name.replace(/<\/?font.*?>/g, "") : "";
  165. let activityTitle = this.$store.state.audioBg.activityTitle;
  166. this.activityTitle = activityTitle ? activityTitle.replace(/<\/?font.*?>/g, "") : "";
  167. this.play = !this.globalBgAudioManager.paused;
  168. this.listenAudio();
  169. },
  170. //音频播放事件
  171. listenAudio() {
  172. // 音频播放
  173. this.globalBgAudioManager.onPlay(async () => {
  174. if (this.isRecord) {
  175. console.log("音频播放");
  176. this.palyTime = 0;
  177. this.backAudioPlay();
  178. this.palyTimeout = setInterval(() => {
  179. this.palyTime++;
  180. }, 1000);
  181. }
  182. this.play = true;
  183. this.$store.commit("audioBg/updateAudioPause", false);
  184. this.isRecord = true;
  185. });
  186. // 音频暂停
  187. this.globalBgAudioManager.onPause(() => {
  188. console.log("音频暂停");
  189. this.backAudioPlay();
  190. clearInterval(this.palyTimeout);
  191. this.play = false;
  192. this.$store.commit("audioBg/updateAudioPause", true);
  193. });
  194. // 音频停止
  195. this.globalBgAudioManager.onStop(() => {
  196. console.log("音频停止");
  197. this.backAudioPlay();
  198. clearInterval(this.palyTimeout);
  199. this.$emit("update:showAudioPop", false);
  200. if (this.isosName == "ios") {
  201. this.play = false;
  202. this.$store.commit("audioBg/removeAudio");
  203. } else {
  204. this.play = false;
  205. this.$store.commit("audioBg/updateAudioPause", true);
  206. }
  207. });
  208. // 音频onEnded
  209. this.globalBgAudioManager.onEnded(() => {
  210. console.log("音频onEnded");
  211. this.backAudioPlay();
  212. clearInterval(this.palyTimeout);
  213. this.$emit("update:showAudioPop", false);
  214. this.$store.commit("audioBg/setAudioEnd", true);
  215. this.$store.commit("audioBg/parseIntAudio", false);
  216. this.$store.commit("audioBg/removeAudio");
  217. });
  218. // 音频onError
  219. this.globalBgAudioManager.onError((e) => {
  220. console.log("音频onError", e);
  221. this.$store.commit("audioBg/removeAudio");
  222. this.$store.commit("audioBg/setMultiple", 1);
  223. uni.showToast({
  224. title: "音频播放错误",
  225. icon: "none",
  226. });
  227. });
  228. // 音频的播放时间更新
  229. this.globalBgAudioManager.onTimeUpdate(() => {
  230. if (this.globalBgAudioManager.src && parseInt(this.globalBgAudioManager.currentTime) !== 0 && !this.isSlide) {
  231. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  232. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  233. }
  234. });
  235. },
  236. // 拖动进度条
  237. handleAudioSliderChangeing(e) {
  238. this.curTime = e.detail.value;
  239. },
  240. // 拖动进度条
  241. handleAudioSliderChange(e) {
  242. const value = e.detail.value;
  243. this.globalBgAudioManager.seek(value);
  244. this.isRecord = false;
  245. setTimeout(() => {
  246. this.$store.commit("audioBg/setSlide", false);
  247. }, 300);
  248. },
  249. // 音频点击暂停播放
  250. handleChangePlayStatus() {
  251. if (!this.globalBgAudioManager.paused) {
  252. this.globalBgAudioManager.pause();
  253. } else {
  254. this.globalBgAudioManager.play();
  255. }
  256. },
  257. // 倍速播放
  258. isTimesHandler(i) {
  259. let index = i == 3 ? 0 : i + 1;
  260. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  261. this.globalBgAudioManager.playbackRate = this.isTimes;
  262. this.globalBgAudioManager.startTime = this.curTime;
  263. if (this.globalBgAudioManager.paused) {
  264. this.globalBgAudioManager.play();
  265. }
  266. this.isRecord = false;
  267. },
  268. //快进 快退
  269. speedReverseHandler(type) {
  270. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  271. isTime = isTime <= 0 ? 0 : isTime >= this.audioTime ? this.audioTime - 1 : isTime;
  272. this.globalBgAudioManager.seek(isTime);
  273. this.isRecord = false;
  274. },
  275. // 播放了记录
  276. backAudioPlay() {
  277. if ((this.$store.state.audioBg.activityId || this.$store.state.audioBg.indexId) && this.palyTime >= 0) {
  278. activity.backAudioPlay({
  279. ActivityId: this.$store.state.audioBg.activityId || this.$store.state.audioBg.indexId || "",
  280. PlaySeconds: this.palyTime,
  281. PageRouter: this.$store.state.pageRouterReport,
  282. });
  283. } else if (this.$store.state.audioBg.reportId && this.palyTime >= 0) {
  284. Reports.reportVoiceHistoryAdd({
  285. ArticleId: this.$store.state.audioBg.reportId,
  286. PlaySeconds: this.palyTime,
  287. PageRouter: this.$store.state.pageRouterReport,
  288. });
  289. }
  290. },
  291. // 手指离开了拖动进度条
  292. touchstartHandler() {
  293. this.$store.commit("audioBg/setSlide", true);
  294. },
  295. // 关闭播放列表
  296. onCloseHandler() {
  297. this.showTabulation = false;
  298. },
  299. palyHandler(item) {
  300. console.log(item,this.$store.state.audioBg.indexId);
  301. if (this.$store.state.audioBg.indexId == item.indexId) {
  302. if (this.globalBgAudioManager.paused) {
  303. this.globalBgAudioManager.play();
  304. } else {
  305. this.globalBgAudioManager.pause();
  306. }
  307. } else {
  308. console.log(112233);
  309. let VoiceList = {
  310. Url: item.Url,
  311. Name: item.Name,
  312. PlaySeconds: item.PlaySeconds,
  313. };
  314. this.$store.commit("audioBg/addAudio", { list: VoiceList, indexId: item.indexId, activityTitle: item.activityTitle });
  315. this.init()
  316. }
  317. },
  318. },
  319. };
  320. </script>
  321. <style scoped lang="scss">
  322. .global-audio-box {
  323. display: flex;
  324. height: 100%;
  325. position: fixed;
  326. width: 100%;
  327. top: 0;
  328. left: 0;
  329. z-index: 111;
  330. .bg-overlay {
  331. width: 100%;
  332. height: 100%;
  333. position: absolute;
  334. top: 0;
  335. left: 0;
  336. background-color: rgba(0, 0, 0, 0.7);
  337. }
  338. .activity-title {
  339. position: relative;
  340. width: 100%;
  341. font-size: 30rpx;
  342. font-weight: 500;
  343. margin-bottom: 35rpx;
  344. padding-right: 30rpx;
  345. .icon-cross {
  346. position: absolute;
  347. right: 0;
  348. top: 50%;
  349. transform: translateY(-50%);
  350. padding: 10rpx;
  351. }
  352. }
  353. .audio-box {
  354. position: absolute;
  355. bottom: 0;
  356. left: 0;
  357. padding: 30rpx;
  358. padding-bottom: constant(safe-area-inset-bottom);
  359. padding-bottom: env(safe-area-inset-bottom);
  360. width: 100%;
  361. background: #ffffff;
  362. box-sizing: border-box;
  363. border-radius: 30rpx 30rpx 0 0;
  364. }
  365. .audio-card {
  366. width: 100%;
  367. height: 282rpx;
  368. background: #f9f9f9;
  369. border-radius: 16rpx;
  370. margin: 0 auto;
  371. padding: 30rpx;
  372. .slider {
  373. width: 100%;
  374. margin: 0;
  375. }
  376. .slider-paly {
  377. display: flex;
  378. height: 80rpx;
  379. align-items: center;
  380. }
  381. .card-title {
  382. color: #376cbb;
  383. font-size: 28rpx;
  384. padding: 0 40rpx;
  385. text-align: center;
  386. margin-bottom: 35rpx;
  387. }
  388. .card-time {
  389. display: flex;
  390. justify-content: space-between;
  391. color: #999999;
  392. font-size: 20rpx;
  393. }
  394. .is-paly-card {
  395. width: 70rpx;
  396. height: 70rpx;
  397. flex-shrink: 0;
  398. margin-left: 30rpx;
  399. image {
  400. width: 70rpx;
  401. height: 70rpx;
  402. }
  403. }
  404. .fast-reverse {
  405. display: flex;
  406. align-items: center;
  407. justify-content: center;
  408. margin-top: 30rpx;
  409. .speed-button {
  410. width: 96rpx;
  411. height: 47rpx;
  412. background: #eaeaea;
  413. border-radius: 8rpx;
  414. text-align: center;
  415. line-height: 47rpx;
  416. margin: 0 70rpx;
  417. }
  418. .speed-img {
  419. width: 50rpx;
  420. height: 50rpx;
  421. }
  422. }
  423. }
  424. }
  425. </style>