index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. this.globalBgAudioManager.onPlay(async () => {
  156. if (this.isRecord) {
  157. console.log("音频播放");
  158. this.palyTime = 0;
  159. this.backAudioPlay();
  160. this.palyTimeout = setInterval(() => {
  161. this.palyTime++;
  162. }, 1000);
  163. }
  164. this.play = true;
  165. this.$store.commit("audioBg/updateAudioPause", false);
  166. this.isRecord = true;
  167. });
  168. this.globalBgAudioManager.onPause(() => {
  169. console.log("音频暂停");
  170. this.backAudioPlay();
  171. clearInterval(this.palyTimeout);
  172. this.play = false;
  173. this.$store.commit("audioBg/updateAudioPause", true);
  174. });
  175. this.globalBgAudioManager.onStop(() => {
  176. console.log("音频停止");
  177. this.backAudioPlay();
  178. clearInterval(this.palyTimeout);
  179. this.$emit("update:showAudioPop", false);
  180. if (this.isosName == "ios") {
  181. this.play = false;
  182. this.$store.commit("audioBg/removeAudio");
  183. } else {
  184. this.play = false;
  185. this.$store.commit("audioBg/updateAudioPause", true);
  186. }
  187. });
  188. this.globalBgAudioManager.onEnded(() => {
  189. console.log("音频onEnded");
  190. this.backAudioPlay();
  191. clearInterval(this.palyTimeout);
  192. this.$emit("update:showAudioPop", false);
  193. this.$store.commit("audioBg/setAudioEnd", true);
  194. this.$store.commit("audioBg/parseIntAudio", false);
  195. this.$store.commit("audioBg/removeAudio");
  196. });
  197. this.globalBgAudioManager.onError((e) => {
  198. console.log("音频onError", e);
  199. this.$store.commit("audioBg/removeAudio");
  200. this.$store.commit("audioBg/setMultiple", 1);
  201. uni.showToast({
  202. title: "音频播放错误",
  203. icon: "none",
  204. });
  205. });
  206. //音频的播放时间更新
  207. this.globalBgAudioManager.onTimeUpdate(() => {
  208. if (this.globalBgAudioManager.src && parseInt(this.globalBgAudioManager.currentTime) !== 0 && !this.isSlide) {
  209. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  210. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  211. }
  212. });
  213. },
  214. //拖动进度条
  215. handleAudioSliderChangeing(e) {
  216. this.curTime = e.detail.value;
  217. },
  218. //拖动进度条
  219. handleAudioSliderChange(e) {
  220. const value = e.detail.value;
  221. this.globalBgAudioManager.seek(value);
  222. this.isRecord = false;
  223. setTimeout(() => {
  224. this.$store.commit("audioBg/setSlide", false);
  225. }, 300);
  226. },
  227. //音频点击暂停播放
  228. handleChangePlayStatus() {
  229. if (!this.globalBgAudioManager.paused) {
  230. this.globalBgAudioManager.pause();
  231. } else {
  232. this.globalBgAudioManager.play();
  233. }
  234. },
  235. //倍速播放
  236. isTimesHandler(i) {
  237. let index = i == 3 ? 0 : i + 1;
  238. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  239. this.globalBgAudioManager.playbackRate = this.isTimes;
  240. this.globalBgAudioManager.startTime = this.curTime;
  241. if (this.globalBgAudioManager.paused) {
  242. this.globalBgAudioManager.play();
  243. }
  244. this.isRecord = false;
  245. },
  246. //快进 快退
  247. speedReverseHandler(type) {
  248. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  249. isTime = isTime <= 0 ? 0 : isTime >= this.audioTime ? this.audioTime - 1 : isTime;
  250. this.globalBgAudioManager.seek(isTime);
  251. this.isRecord = false;
  252. },
  253. //播放了记录
  254. backAudioPlay() {
  255. if ((this.$store.state.audioBg.activityId || this.$store.state.audioBg.indexId) && this.palyTime >= 0) {
  256. activity.backAudioPlay({
  257. ActivityId: this.$store.state.audioBg.activityId || this.$store.state.audioBg.indexId,
  258. PlaySeconds: this.palyTime,
  259. });
  260. }
  261. },
  262. touchstartHandler() {
  263. this.$store.commit("audioBg/setSlide", true);
  264. },
  265. },
  266. };
  267. </script>
  268. <style scoped lang="scss">
  269. .global-audio-box {
  270. display: flex;
  271. height: 100%;
  272. position: fixed;
  273. width: 100%;
  274. top: 0;
  275. left: 0;
  276. z-index: 111;
  277. .bg-overlay {
  278. width: 100%;
  279. height: 100%;
  280. position: absolute;
  281. top: 0;
  282. left: 0;
  283. background-color: rgba(0, 0, 0, 0.7);
  284. }
  285. .activity-title {
  286. position: relative;
  287. width: 100%;
  288. font-size: 30rpx;
  289. font-weight: 500;
  290. margin-bottom: 35rpx;
  291. padding-right: 30rpx;
  292. .icon-cross {
  293. position: absolute;
  294. right: 0;
  295. top: 50%;
  296. transform: translateY(-50%);
  297. padding: 10rpx;
  298. }
  299. }
  300. .audio-box {
  301. position: absolute;
  302. bottom: 0;
  303. left: 0;
  304. padding: 30rpx;
  305. padding-bottom: constant(safe-area-inset-bottom);
  306. padding-bottom: env(safe-area-inset-bottom);
  307. width: 100%;
  308. background: #ffffff;
  309. box-sizing: border-box;
  310. border-radius: 30rpx 30rpx 0 0;
  311. }
  312. .audio-card {
  313. width: 100%;
  314. height: 282rpx;
  315. background: #f9f9f9;
  316. border-radius: 16rpx;
  317. margin: 0 auto;
  318. padding: 30rpx;
  319. .slider {
  320. width: 100%;
  321. margin: 0;
  322. }
  323. .slider-paly {
  324. display: flex;
  325. height: 80rpx;
  326. align-items: center;
  327. }
  328. .card-title {
  329. color: #3385ff;
  330. font-size: 28rpx;
  331. padding: 0 40rpx;
  332. text-align: center;
  333. margin-bottom: 35rpx;
  334. }
  335. .card-time {
  336. display: flex;
  337. justify-content: space-between;
  338. color: #999999;
  339. font-size: 20rpx;
  340. }
  341. .is-paly-card {
  342. width: 70rpx;
  343. height: 70rpx;
  344. flex-shrink: 0;
  345. margin-left: 30rpx;
  346. image {
  347. width: 70rpx;
  348. height: 70rpx;
  349. }
  350. }
  351. .fast-reverse {
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. margin-top: 30rpx;
  356. .speed-button {
  357. width: 96rpx;
  358. height: 47rpx;
  359. background: #eaeaea;
  360. border-radius: 8rpx;
  361. text-align: center;
  362. line-height: 47rpx;
  363. margin: 0 70rpx;
  364. }
  365. .speed-img {
  366. width: 50rpx;
  367. height: 50rpx;
  368. }
  369. }
  370. }
  371. }
  372. </style>