index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. </template>
  31. <script>
  32. import { activity } from "@/config/api";
  33. export default {
  34. name: "",
  35. filters: {
  36. formatVoiceTime(e) {
  37. let m = parseInt(e / 60);
  38. let s = parseInt(e % 60);
  39. return `${m > 9 ? m : "0" + m}:${s > 9 ? s : "0" + s}`;
  40. },
  41. },
  42. components: {},
  43. props: {
  44. showAudioPop: {
  45. type: Boolean,
  46. default: false,
  47. required: true,
  48. },
  49. },
  50. data() {
  51. return {
  52. curTime: 0,
  53. audioTime: 0, //当前音频总时长
  54. title: "", //当前音频标题
  55. activityTitle: "", //当前活动标题
  56. play: false,
  57. isEnded: 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. });
  104. this.globalBgAudioManager.onPause(() => {
  105. console.log("音频暂停");
  106. this.play = false;
  107. this.$store.commit("audioBg/updateAudioPause", true);
  108. });
  109. this.globalBgAudioManager.onStop(() => {
  110. console.log("音频停止");
  111. this.play = false;
  112. this.$store.commit("audioBg/updateAudioPause", true);
  113. });
  114. this.globalBgAudioManager.onEnded(() => {
  115. console.log("音频onEnded");
  116. this.play = false;
  117. this.$store.commit("audioBg/updateAudioPause", true);
  118. });
  119. this.globalBgAudioManager.onError((e) => {
  120. console.log("音频onError", e);
  121. this.$store.commit("audioBg/removeAudio");
  122. uni.showToast({
  123. title: "音频播放错误",
  124. icon: "none",
  125. });
  126. });
  127. //音频的播放时间更新
  128. this.globalBgAudioManager.onTimeUpdate(() => {
  129. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  130. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  131. });
  132. },
  133. //拖动进度条
  134. handleAudioSliderChange(e) {
  135. const value = e.detail.value;
  136. this.globalBgAudioManager.seek(value);
  137. },
  138. //音频点击暂停播放
  139. handleChangePlayStatus() {
  140. if (this.globalBgAudioManager.src) {
  141. if (!this.globalBgAudioManager.paused) {
  142. this.globalBgAudioManager.pause();
  143. } else {
  144. this.globalBgAudioManager.play();
  145. }
  146. } else {
  147. this.init();
  148. }
  149. },
  150. },
  151. };
  152. </script>
  153. <style scoped lang="scss">
  154. .global-audio-box {
  155. display: flex;
  156. height: 100%;
  157. position: fixed;
  158. width: 100%;
  159. top: 0;
  160. left: 0;
  161. z-index: 11;
  162. .bg-overlay {
  163. width: 100%;
  164. height: 100%;
  165. position: absolute;
  166. top: 0;
  167. left: 0;
  168. background-color: rgba(0, 0, 0, 0.7);
  169. }
  170. .activity-title {
  171. position: relative;
  172. width: 100%;
  173. font-size: 30rpx;
  174. font-weight: 500;
  175. margin-bottom: 35rpx;
  176. text-align: center;
  177. padding-right: 30rpx;
  178. .icon-cross {
  179. position: absolute;
  180. right: 0;
  181. top: 50%;
  182. transform: translateY(-50%);
  183. padding: 10rpx;
  184. }
  185. }
  186. .audio-box {
  187. position: absolute;
  188. bottom: 0;
  189. left: 0;
  190. padding-bottom: constant(safe-area-inset-bottom);
  191. padding-bottom: env(safe-area-inset-bottom);
  192. width: 100%;
  193. height: 460rpx;
  194. background: #ffffff;
  195. padding: 30rpx;
  196. box-sizing: border-box;
  197. border-radius: 30rpx 30rpx 0 0;
  198. }
  199. .audio-card {
  200. width: 100%;
  201. height: 282rpx;
  202. background: #f5f9ff;
  203. border-radius: 16rpx;
  204. margin: 0 auto;
  205. padding: 30rpx;
  206. .slider {
  207. width: 100%;
  208. margin: 0;
  209. }
  210. .card-title {
  211. color: #3385ff;
  212. font-size: 28rpx;
  213. padding: 0 66rpx;
  214. text-align: center;
  215. margin-bottom: 35rpx;
  216. }
  217. .card-time {
  218. display: flex;
  219. justify-content: space-between;
  220. color: #999999;
  221. font-size: 20rpx;
  222. }
  223. .is-paly-card {
  224. width: 100%;
  225. text-align: center;
  226. image {
  227. margin-top: 5rpx;
  228. width: 100rpx;
  229. height: 100rpx;
  230. }
  231. }
  232. }
  233. }
  234. </style>