index.vue 8.7 KB

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