index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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" @change="handleAudioSliderChange($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. // get() {
  90. // return this.$store.state.audioBg.multiple;
  91. // },
  92. // set(val) {
  93. // return this.$store.state.audioBg.multiple = val;
  94. // },
  95. },
  96. },
  97. watch: {
  98. audioInit: {
  99. handler(nval) {
  100. this.init();
  101. },
  102. immediate: true,
  103. },
  104. },
  105. created() {},
  106. mounted() {
  107. this.init();
  108. },
  109. methods: {
  110. //点击隐藏事件
  111. isShowMaskHandler() {
  112. this.$emit("update:showAudioPop", false);
  113. },
  114. //数据初次加载
  115. init() {
  116. let delyTime = 0;
  117. if (this.showAudioPop) {
  118. console.log(123,'showAudioPop');
  119. this.globalBgAudioManager.stop();
  120. delyTime = 300;
  121. }
  122. const curAudio = this.$store.state.audioBg.list;
  123. setTimeout(() => {
  124. if (this.globalBgAudioManager.src != curAudio.Url) {
  125. this.globalBgAudioManager.src = curAudio.Url;
  126. this.globalBgAudioManager.title = curAudio.Name;
  127. this.$store.commit("audioBg/updateAudioTime", 0);
  128. this.$store.commit("audioBg/setMultiple", 1);
  129. }
  130. this.globalBgAudioManager.playbackRate = this.isTimes;
  131. this.audioTime = curAudio.PlaySeconds;
  132. this.title = curAudio.Name;
  133. this.activityTitle = this.$store.state.audioBg.activityTitle;
  134. this.curTime = this.$store.state.audioBg.curTime;
  135. this.play = !this.globalBgAudioManager.paused;
  136. this.listenAudio();
  137. }, delyTime);
  138. },
  139. //音频播放事件
  140. listenAudio() {
  141. console.log('执行了');
  142. this.globalBgAudioManager.onPlay(async () => {
  143. console.log("onPlay");
  144. this.play = true;
  145. this.$store.commit("audioBg/updateAudioPause", false);
  146. if (this.$store.state.audioBg.activityId) {
  147. activity.backAudioPlay({ ActivityId: this.$store.state.audioBg.activityId });
  148. }
  149. });
  150. this.globalBgAudioManager.onPause(() => {
  151. console.log("音频暂停");
  152. this.play = false;
  153. this.$store.commit("audioBg/updateAudioPause", true);
  154. });
  155. this.globalBgAudioManager.onStop(() => {
  156. console.log("音频停止");
  157. this.$store.commit("audioBg/removeAudio");
  158. // uni.getSystemInfo({
  159. // success: (res) => {
  160. // if (res.osName == "ios") {
  161. // console.log("ios");
  162. // this.play = false;
  163. // this.$store.commit("audioBg/removeAudio");
  164. // } else {
  165. // this.play = false;
  166. // this.$store.commit("audioBg/updateAudioPause", true);
  167. // }
  168. // },
  169. // });
  170. });
  171. this.globalBgAudioManager.onEnded(() => {
  172. console.log("音频onEnded");
  173. this.play = false;
  174. this.$store.commit("audioBg/updateAudioPause", true);
  175. this.$store.commit("audioBg/setMultiple", 1);
  176. });
  177. this.globalBgAudioManager.onError((e) => {
  178. console.log("音频onError", e);
  179. this.$store.commit("audioBg/removeAudio");
  180. this.$store.commit("audioBg/setMultiple", 1);
  181. uni.showToast({
  182. title: "音频播放错误",
  183. icon: "none",
  184. });
  185. });
  186. //音频的播放时间更新
  187. this.globalBgAudioManager.onTimeUpdate(() => {
  188. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  189. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  190. });
  191. },
  192. //拖动进度条
  193. handleAudioSliderChange(e) {
  194. const value = e.detail.value;
  195. console.log(value, "value");
  196. this.globalBgAudioManager.seek(value);
  197. },
  198. //音频点击暂停播放
  199. handleChangePlayStatus() {
  200. if (this.globalBgAudioManager.src) {
  201. if (!this.globalBgAudioManager.paused) {
  202. this.globalBgAudioManager.pause();
  203. } else {
  204. this.globalBgAudioManager.play();
  205. }
  206. } else {
  207. this.init();
  208. }
  209. },
  210. //倍速播放
  211. isTimesHandler(i) {
  212. let index = i == 3 ? 0 : i + 1;
  213. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  214. this.globalBgAudioManager.playbackRate = this.isTimes;
  215. this.globalBgAudioManager.startTime = this.curTime;
  216. this.play = true;
  217. },
  218. //快进 快退
  219. speedReverseHandler(type) {
  220. if (!this.globalBgAudioManager.src) {
  221. this.init();
  222. }
  223. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  224. isTime = isTime <= 0 ? 0 : isTime;
  225. this.globalBgAudioManager.seek(isTime);
  226. },
  227. },
  228. };
  229. </script>
  230. <style scoped lang="scss">
  231. .global-audio-box {
  232. display: flex;
  233. height: 100%;
  234. position: fixed;
  235. width: 100%;
  236. top: 0;
  237. left: 0;
  238. z-index: 11;
  239. .bg-overlay {
  240. width: 100%;
  241. height: 100%;
  242. position: absolute;
  243. top: 0;
  244. left: 0;
  245. background-color: rgba(0, 0, 0, 0.7);
  246. }
  247. .activity-title {
  248. position: relative;
  249. width: 100%;
  250. font-size: 30rpx;
  251. font-weight: 500;
  252. margin-bottom: 35rpx;
  253. padding-right: 30rpx;
  254. .icon-cross {
  255. position: absolute;
  256. right: 0;
  257. top: 50%;
  258. transform: translateY(-50%);
  259. padding: 10rpx;
  260. }
  261. }
  262. .audio-box {
  263. position: absolute;
  264. bottom: 0;
  265. left: 0;
  266. padding: 30rpx;
  267. padding-bottom: constant(safe-area-inset-bottom);
  268. padding-bottom: env(safe-area-inset-bottom);
  269. width: 100%;
  270. background: #ffffff;
  271. box-sizing: border-box;
  272. border-radius: 30rpx 30rpx 0 0;
  273. }
  274. .audio-card {
  275. width: 100%;
  276. height: 282rpx;
  277. background: #f9f9f9;
  278. border-radius: 16rpx;
  279. margin: 0 auto;
  280. padding: 30rpx;
  281. .slider {
  282. width: 100%;
  283. margin: 0;
  284. }
  285. .slider-paly {
  286. display: flex;
  287. height: 80rpx;
  288. align-items: center;
  289. }
  290. .card-title {
  291. color: #3385ff;
  292. font-size: 28rpx;
  293. padding: 0 40rpx;
  294. text-align: center;
  295. margin-bottom: 35rpx;
  296. }
  297. .card-time {
  298. display: flex;
  299. justify-content: space-between;
  300. color: #999999;
  301. font-size: 20rpx;
  302. }
  303. .is-paly-card {
  304. width: 70rpx;
  305. height: 70rpx;
  306. flex-shrink: 0;
  307. margin-left: 30rpx;
  308. image {
  309. width: 70rpx;
  310. height: 70rpx;
  311. }
  312. }
  313. .fast-reverse {
  314. display: flex;
  315. align-items: center;
  316. justify-content: center;
  317. margin-top: 30rpx;
  318. .speed-button {
  319. width: 96rpx;
  320. height: 47rpx;
  321. background: #eaeaea;
  322. border-radius: 8rpx;
  323. text-align: center;
  324. line-height: 47rpx;
  325. margin: 0 70rpx;
  326. }
  327. .speed-img {
  328. width: 50rpx;
  329. height: 50rpx;
  330. }
  331. }
  332. }
  333. }
  334. </style>