index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 text_oneLine">
  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. <slider activeColor="#3385FF" :max="audioTime" :value="curTime" @change="handleAudioSliderChange($event)" block-size="16" class="slider" />
  16. <view class="card-time">
  17. <text class="time">{{ curTime | formatVoiceTime }}</text>
  18. <text class="time">{{ audioTime | formatVoiceTime }}</text>
  19. </view>
  20. <view class="is-paly-card">
  21. <image
  22. @click.stop="handleChangePlayStatus"
  23. class=""
  24. :src="play ? 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/suspend_icon.png' : 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/play_icon.png'"
  25. ></image>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. <!-- </van-overlay> -->
  31. </template>
  32. <script>
  33. import { activity } from "@/config/api";
  34. export default {
  35. name: "",
  36. filters: {
  37. formatVoiceTime(e) {
  38. let m = parseInt(e / 60);
  39. let s = parseInt(e % 60);
  40. return `${m > 9 ? m : "0" + m}:${s > 9 ? s : "0" + s}`;
  41. },
  42. },
  43. components: {},
  44. props: {
  45. showAudioPop: {
  46. type: Boolean,
  47. default: false,
  48. required: true,
  49. },
  50. },
  51. data() {
  52. return {
  53. curTime: 0,
  54. audioTime: 0, //当前音频总时长
  55. title: "", //当前音频标题
  56. activityTitle: "", //当前活动标题
  57. play: false,
  58. };
  59. },
  60. computed: {
  61. audioInit() {
  62. return {
  63. activityId: this.$store.state.audioBg.activityId,
  64. };
  65. },
  66. },
  67. watch: {
  68. audioInit: {
  69. handler(nval) {
  70. this.init();
  71. },
  72. immediate: true,
  73. },
  74. },
  75. created() {},
  76. mounted() {
  77. this.init();
  78. },
  79. methods: {
  80. //点击隐藏事件
  81. isShowMaskHandler() {
  82. this.$emit("update:showAudioPop", false);
  83. },
  84. //数据初次加载
  85. init() {
  86. const curAudio = this.$store.state.audioBg.list;
  87. if (this.globalBgAudioManager.src != curAudio.Url) {
  88. this.globalBgAudioManager.src = curAudio.Url;
  89. this.globalBgAudioManager.title = curAudio.Name;
  90. }
  91. this.audioTime = curAudio.PlaySeconds;
  92. this.title = curAudio.Name;
  93. this.activityTitle = this.$store.state.audioBg.activityTitle;
  94. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  95. this.play = !this.globalBgAudioManager.paused;
  96. this.listenAudio();
  97. },
  98. //音频播放事件
  99. listenAudio() {
  100. this.globalBgAudioManager.onPlay(async () => {
  101. this.play = true;
  102. this.$store.commit("audioBg/updateAudioPause", false);
  103. const res = await activity.backAudioPlay({
  104. ActivityId: this.$store.state.audioBg.activityId,
  105. });
  106. });
  107. this.globalBgAudioManager.onPause(() => {
  108. console.log("音频暂停");
  109. this.play = false;
  110. this.$store.commit("audioBg/updateAudioPause", true);
  111. });
  112. this.globalBgAudioManager.onStop(() => {
  113. console.log("音频停止");
  114. this.$store.commit("audioBg/removeAudio");
  115. });
  116. this.globalBgAudioManager.onEnded(() => {
  117. console.log("音频onEnded");
  118. this.$store.commit("audioBg/removeAudio");
  119. });
  120. this.globalBgAudioManager.onError((e) => {
  121. console.log("音频onError", e);
  122. this.$store.commit("audioBg/removeAudio");
  123. uni.showToast({
  124. title: "音频播放错误",
  125. icon: "none",
  126. });
  127. });
  128. //音频的播放时间更新
  129. this.globalBgAudioManager.onTimeUpdate(() => {
  130. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  131. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  132. });
  133. },
  134. //拖动进度条
  135. handleAudioSliderChange(e) {
  136. const value = e.detail.value;
  137. this.globalBgAudioManager.seek(value);
  138. },
  139. //音频点击暂停播放
  140. handleChangePlayStatus() {
  141. if (!this.globalBgAudioManager.paused) {
  142. this.globalBgAudioManager.pause();
  143. } else {
  144. this.globalBgAudioManager.play();
  145. }
  146. },
  147. },
  148. };
  149. </script>
  150. <style scoped lang="scss">
  151. .global-audio-box {
  152. display: flex;
  153. height: 100%;
  154. position: fixed;
  155. width: 100%;
  156. top: 0;
  157. left: 0;
  158. z-index: 11;
  159. .bg-overlay {
  160. width: 100%;
  161. height: 100%;
  162. position: absolute;
  163. top: 0;
  164. left: 0;
  165. background-color: rgba(0, 0, 0, 0.7);
  166. }
  167. .activity-title {
  168. position: relative;
  169. width: 100%;
  170. font-size: 30rpx;
  171. font-weight: 500;
  172. margin-bottom: 35rpx;
  173. text-align: center;
  174. padding-right: 30rpx;
  175. .icon-cross {
  176. position: absolute;
  177. right: 0;
  178. top: 50%;
  179. transform: translateY(-50%);
  180. padding: 10rpx;
  181. }
  182. }
  183. .audio-box {
  184. position: absolute;
  185. bottom: 0;
  186. left: 0;
  187. padding-bottom: constant(safe-area-inset-bottom);
  188. padding-bottom: env(safe-area-inset-bottom);
  189. width: 100%;
  190. height: 460rpx;
  191. background: #ffffff;
  192. padding: 30rpx;
  193. box-sizing: border-box;
  194. border-radius: 30rpx 30rpx 0 0;
  195. }
  196. .audio-card {
  197. width: 100%;
  198. height: 282rpx;
  199. background: #f5f9ff;
  200. border-radius: 16rpx;
  201. margin: 0 auto;
  202. padding: 30rpx;
  203. .slider {
  204. width: 100%;
  205. margin: 0;
  206. }
  207. .card-title {
  208. color: #3385ff;
  209. font-size: 26rpx;
  210. padding: 0 66rpx;
  211. text-align: center;
  212. margin-bottom: 35rpx;
  213. }
  214. .card-time {
  215. display: flex;
  216. justify-content: space-between;
  217. color: #999999;
  218. font-size: 20rpx;
  219. }
  220. .is-paly-card {
  221. width: 100%;
  222. text-align: center;
  223. image {
  224. margin-top: 5rpx;
  225. width: 100rpx;
  226. height: 100rpx;
  227. }
  228. }
  229. }
  230. }
  231. </style>