index.vue 9.3 KB

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