index.vue 11 KB

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