index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.$store.commit("audioBg/removeAudio");
  159. this.$store.commit("audioBg/parseIntAudio", false);
  160. });
  161. this.globalBgAudioManager.onError((e) => {
  162. console.log("音频onError", e);
  163. this.$store.commit("audioBg/removeAudio");
  164. this.$store.commit("audioBg/setMultiple", 1);
  165. uni.showToast({
  166. title: "音频播放错误",
  167. icon: "none",
  168. });
  169. });
  170. //音频的播放时间更新
  171. this.globalBgAudioManager.onTimeUpdate(() => {
  172. if (this.globalBgAudioManager.src && parseInt(this.globalBgAudioManager.currentTime) !== 0) {
  173. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  174. if (this.audioTime - nvthis.curTimeal < 10) {
  175. this.globalBgAudioManager.playbackRate = 1;
  176. }
  177. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  178. }
  179. });
  180. },
  181. //拖动进度条
  182. handleAudioSliderChangeing(e) {
  183. this.curTime = e.detail.value;
  184. this.globalBgAudioManager.seek(this.curTime);
  185. },
  186. //音频点击暂停播放
  187. handleChangePlayStatus() {
  188. if (!this.globalBgAudioManager.paused) {
  189. this.globalBgAudioManager.pause();
  190. } else {
  191. this.globalBgAudioManager.play();
  192. }
  193. },
  194. //倍速播放
  195. isTimesHandler(i) {
  196. console.log(this.curTime);
  197. let index = i == 3 ? 0 : i + 1;
  198. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  199. this.globalBgAudioManager.playbackRate = this.isTimes;
  200. this.globalBgAudioManager.startTime = this.curTime;
  201. if (this.globalBgAudioManager.paused) {
  202. this.globalBgAudioManager.play();
  203. }
  204. },
  205. //快进 快退
  206. speedReverseHandler(type) {
  207. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  208. isTime = isTime <= 0 ? 0 : isTime >= this.audioTime ? this.audioTime - 1 : isTime;
  209. this.globalBgAudioManager.seek(isTime);
  210. },
  211. },
  212. };
  213. </script>
  214. <style scoped lang="scss">
  215. .global-audio-box {
  216. display: flex;
  217. height: 100%;
  218. position: fixed;
  219. width: 100%;
  220. top: 0;
  221. left: 0;
  222. z-index: 11;
  223. .bg-overlay {
  224. width: 100%;
  225. height: 100%;
  226. position: absolute;
  227. top: 0;
  228. left: 0;
  229. background-color: rgba(0, 0, 0, 0.7);
  230. }
  231. .activity-title {
  232. position: relative;
  233. width: 100%;
  234. font-size: 30rpx;
  235. font-weight: 500;
  236. margin-bottom: 35rpx;
  237. padding-right: 30rpx;
  238. .icon-cross {
  239. position: absolute;
  240. right: 0;
  241. top: 50%;
  242. transform: translateY(-50%);
  243. padding: 10rpx;
  244. }
  245. }
  246. .audio-box {
  247. position: absolute;
  248. bottom: 0;
  249. left: 0;
  250. padding: 30rpx;
  251. padding-bottom: constant(safe-area-inset-bottom);
  252. padding-bottom: env(safe-area-inset-bottom);
  253. width: 100%;
  254. background: #ffffff;
  255. box-sizing: border-box;
  256. border-radius: 30rpx 30rpx 0 0;
  257. }
  258. .audio-card {
  259. width: 100%;
  260. height: 282rpx;
  261. background: #f9f9f9;
  262. border-radius: 16rpx;
  263. margin: 0 auto;
  264. padding: 30rpx;
  265. .slider {
  266. width: 100%;
  267. margin: 0;
  268. }
  269. .slider-paly {
  270. display: flex;
  271. height: 80rpx;
  272. align-items: center;
  273. }
  274. .card-title {
  275. color: #3385ff;
  276. font-size: 28rpx;
  277. padding: 0 40rpx;
  278. text-align: center;
  279. margin-bottom: 35rpx;
  280. }
  281. .card-time {
  282. display: flex;
  283. justify-content: space-between;
  284. color: #999999;
  285. font-size: 20rpx;
  286. }
  287. .is-paly-card {
  288. width: 70rpx;
  289. height: 70rpx;
  290. flex-shrink: 0;
  291. margin-left: 30rpx;
  292. image {
  293. width: 70rpx;
  294. height: 70rpx;
  295. }
  296. }
  297. .fast-reverse {
  298. display: flex;
  299. align-items: center;
  300. justify-content: center;
  301. margin-top: 30rpx;
  302. .speed-button {
  303. width: 96rpx;
  304. height: 47rpx;
  305. background: #eaeaea;
  306. border-radius: 8rpx;
  307. text-align: center;
  308. line-height: 47rpx;
  309. margin: 0 70rpx;
  310. }
  311. .speed-img {
  312. width: 50rpx;
  313. height: 50rpx;
  314. }
  315. }
  316. }
  317. }
  318. </style>