1.vue 9.1 KB

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