slider.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. <view>
  18. <movable-area class="slider" id="movable-area">
  19. <movable-view
  20. class="time-button"
  21. animation="false"
  22. @change="handleSlideChange"
  23. @touchend="onTouchEnd"
  24. @htouchmove="onHtouchmove"
  25. direction="horizontal"
  26. id="movable-view"
  27. :x="movableViewX"
  28. >
  29. </movable-view>
  30. <progress activeColor="#666" backgroundColor="#999" borderRadius="2" class="progress" :percent="progress" strokeWidth="2"></progress>
  31. </movable-area>
  32. </view>
  33. <view class="card-time">
  34. <text class="time">{{ curTime | formatVoiceTime }}</text>
  35. <text class="time">{{ audioTime | formatVoiceTime }}</text>
  36. </view>
  37. </view>
  38. <view class="is-paly-card">
  39. <image
  40. @click.stop="handleChangePlayStatus"
  41. :src="play ? 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/play_icon.gif' : 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/pause_icon.png'"
  42. ></image>
  43. </view>
  44. </view>
  45. <view class="fast-reverse">
  46. <image @click="speedReverseHandler('reverse')" class="speed-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/fastReverse_icon.png"></image>
  47. <block v-for="(item, index) in timesTheSpeed" :key="item.value">
  48. <view class="speed-button" v-if="isTimes == item.value" @click="isTimesHandler(index)">
  49. {{ item.name }}
  50. </view>
  51. </block>
  52. <image @click="speedReverseHandler('speed')" class="speed-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/speed_icon.png"></image>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. import { activity } from "@/config/api";
  60. export default {
  61. name: "",
  62. filters: {
  63. formatVoiceTime(e) {
  64. let m = parseInt(e / 60);
  65. let s = parseInt(e % 60);
  66. return `${m > 9 ? m : "0" + m}:${s > 9 ? s : "0" + s}`;
  67. },
  68. },
  69. components: {},
  70. props: {
  71. showAudioPop: {
  72. type: Boolean,
  73. default: false,
  74. required: true,
  75. },
  76. },
  77. data() {
  78. return {
  79. movableViewX: 0,
  80. currentTime: 0,
  81. progress: 0,
  82. moveAreaW: 0,
  83. moveboxW: 0,
  84. curTime: 0,
  85. audioTime: 0, //当前音频总时长
  86. title: "", //当前音频标题
  87. activityTitle: "", //当前活动标题
  88. play: false,
  89. isEnded: false,
  90. isMoving: false,
  91. timesTheSpeed: [
  92. { name: "倍速", value: 1 },
  93. { name: "1.25倍", value: 1.25 },
  94. { name: "1.5倍", value: 1.5 },
  95. { name: "2倍", value: 2 },
  96. ],
  97. };
  98. },
  99. computed: {
  100. //重新
  101. audioInit() {
  102. return {
  103. activityId: this.$store.state.audioBg.activityId,
  104. reportId: this.$store.state.audioBg.reportId,
  105. };
  106. },
  107. // 几倍的播放速度
  108. isTimes() {
  109. return this.$store.state.audioBg.multiple;
  110. },
  111. },
  112. watch: {
  113. audioInit: {
  114. handler(nval) {
  115. this.init();
  116. const query = uni.createSelectorQuery().in(this);
  117. query
  118. .select("#movable-area")
  119. .boundingClientRect((data) => {
  120. this.moveAreaW = data && data.width;
  121. })
  122. .exec();
  123. query
  124. .select("#movable-view")
  125. .boundingClientRect((data) => {
  126. this.moveboxW = data && data.width;
  127. })
  128. .exec();
  129. },
  130. immediate: true,
  131. },
  132. },
  133. created() {},
  134. mounted() {
  135. console.log(123);
  136. },
  137. methods: {
  138. handleSlideChange(e) {
  139. console.log(e);
  140. this.movableViewX = e.detail.x;
  141. this.progress = Math.round((this.movableViewX / (this.moveAreaW - this.moveboxW)) * 100);
  142. if (this.isMoving) {
  143. this.curTime = Math.round(this.globalBgAudioManager.duration * (this.progress / 100));
  144. }
  145. },
  146. // 用户拖动进度条结束时,重新给背景音乐的各项赋值
  147. onTouchEnd() {
  148. const isTime = parseInt(this.globalBgAudioManager.duration * (this.progress / 100));
  149. this.globalBgAudioManager.seek(isTime);
  150. setTimeout(() => {
  151. this.isMoving = false;
  152. }, 0);
  153. },
  154. onHtouchmove() {
  155. this.isMoving = true;
  156. },
  157. //点击隐藏事件
  158. isShowMaskHandler() {
  159. this.$emit("update:showAudioPop", false);
  160. },
  161. //数据初次加载
  162. init() {
  163. const curAudio = this.$store.state.audioBg.list;
  164. if (this.globalBgAudioManager.src != curAudio.Url && curAudio.Url) {
  165. this.$store.commit("audioBg/setMultiple", 1);
  166. this.$store.commit("audioBg/updateAudioTime", 0);
  167. this.globalBgAudioManager.src = curAudio.Url;
  168. this.globalBgAudioManager.title = curAudio.Name;
  169. this.globalBgAudioManager.startTime = 0;
  170. this.globalBgAudioManager.playbackRate = this.isTimes;
  171. this.curTime = 0;
  172. this.progress = 0;
  173. this.movableViewX = 0;
  174. console.log(this.movableViewX, " this.movableViewX = 0;");
  175. } else {
  176. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  177. }
  178. console.log(this.movableViewX, " this.movableViewX = 0;");
  179. this.audioTime = curAudio.PlaySeconds;
  180. this.title = curAudio.Name;
  181. this.activityTitle = this.$store.state.audioBg.activityTitle;
  182. this.play = !this.globalBgAudioManager.paused;
  183. this.listenAudio();
  184. },
  185. //音频播放事件
  186. listenAudio() {
  187. this.globalBgAudioManager.onPlay(async () => {
  188. console.log("音频播放");
  189. this.play = true;
  190. this.$store.commit("audioBg/updateAudioPause", false);
  191. if (this.$store.state.audioBg.activityId) {
  192. activity.backAudioPlay({ ActivityId: this.$store.state.audioBg.activityId });
  193. }
  194. });
  195. this.globalBgAudioManager.onPause(() => {
  196. console.log("音频暂停");
  197. this.play = false;
  198. this.$store.commit("audioBg/updateAudioPause", true);
  199. });
  200. this.globalBgAudioManager.onStop(() => {
  201. console.log("音频停止");
  202. this.$emit("update:showAudioPop", false);
  203. uni.getSystemInfo({
  204. success: (res) => {
  205. if (res.osName == "ios") {
  206. this.play = false;
  207. this.$store.commit("audioBg/removeAudio");
  208. } else {
  209. this.play = false;
  210. this.$store.commit("audioBg/updateAudioPause", true);
  211. }
  212. },
  213. });
  214. });
  215. this.globalBgAudioManager.onEnded(() => {
  216. console.log("音频onEnded");
  217. this.$emit("update:showAudioPop", false);
  218. this.$store.commit("audioBg/removeAudio");
  219. this.$store.commit("audioBg/parseIntAudio", false);
  220. });
  221. this.globalBgAudioManager.onError((e) => {
  222. console.log("音频onError", e);
  223. this.$store.commit("audioBg/removeAudio");
  224. this.$store.commit("audioBg/setMultiple", 1);
  225. uni.showToast({
  226. title: "音频播放错误",
  227. icon: "none",
  228. });
  229. });
  230. //音频的播放时间更新
  231. this.globalBgAudioManager.onTimeUpdate(() => {
  232. if (!this.isMoving) {
  233. if (this.globalBgAudioManager.src && parseInt(this.globalBgAudioManager.currentTime) !== 0) {
  234. this.movableViewX = Math.round(((this.moveAreaW - this.moveboxW) * parseInt(this.globalBgAudioManager.currentTime)) / this.globalBgAudioManager.duration);
  235. this.progress = Math.round((100 * parseInt(this.globalBgAudioManager.currentTime)) / this.globalBgAudioManager.duration);
  236. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  237. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  238. }
  239. }
  240. });
  241. },
  242. //拖动进度条
  243. // handleAudioSliderChangeing(e) {
  244. // this.curTime = e.detail.value;
  245. // this.globalBgAudioManager.seek(this.curTime);
  246. // },
  247. //音频点击暂停播放
  248. handleChangePlayStatus() {
  249. if (!this.globalBgAudioManager.paused) {
  250. this.globalBgAudioManager.pause();
  251. } else {
  252. this.globalBgAudioManager.play();
  253. }
  254. },
  255. //倍速播放
  256. isTimesHandler(i) {
  257. console.log(this.curTime);
  258. let index = i == 3 ? 0 : i + 1;
  259. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  260. this.globalBgAudioManager.playbackRate = this.isTimes;
  261. this.globalBgAudioManager.startTime = this.curTime;
  262. if (this.globalBgAudioManager.paused) {
  263. this.globalBgAudioManager.play();
  264. }
  265. },
  266. //快进 快退
  267. speedReverseHandler(type) {
  268. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  269. isTime = isTime <= 0 ? 0 : isTime >= this.audioTime ? this.audioTime - 1 : isTime;
  270. this.globalBgAudioManager.seek(isTime);
  271. },
  272. },
  273. };
  274. </script>
  275. <style>
  276. .slider {
  277. height: 40rpx;
  278. margin: 0 32rpx;
  279. position: relative;
  280. width: 528rpx;
  281. }
  282. .slider .time-button {
  283. background: #333;
  284. border-radius: 30rpx;
  285. color: #fff;
  286. height: 30rpx;
  287. text-align: center;
  288. width: 30rpx;
  289. z-index: 1;
  290. }
  291. .slider .progress {
  292. height: 4rpx;
  293. left: 0;
  294. position: absolute;
  295. top: 18rpx;
  296. width: 528rpx;
  297. }
  298. .play-button {
  299. -webkit-box-pack: center;
  300. -webkit-box-align: center;
  301. align-items: center;
  302. display: flex;
  303. justify-content: center;
  304. margin-top: 52rpx;
  305. }
  306. </style>
  307. <style scoped lang="scss">
  308. .global-audio-box {
  309. display: flex;
  310. height: 100%;
  311. position: fixed;
  312. width: 100%;
  313. top: 0;
  314. left: 0;
  315. z-index: 11;
  316. .bg-overlay {
  317. width: 100%;
  318. height: 100%;
  319. position: absolute;
  320. top: 0;
  321. left: 0;
  322. background-color: rgba(0, 0, 0, 0.7);
  323. }
  324. .activity-title {
  325. position: relative;
  326. width: 100%;
  327. font-size: 30rpx;
  328. font-weight: 500;
  329. margin-bottom: 35rpx;
  330. padding-right: 30rpx;
  331. .icon-cross {
  332. position: absolute;
  333. right: 0;
  334. top: 50%;
  335. transform: translateY(-50%);
  336. padding: 10rpx;
  337. }
  338. }
  339. .audio-box {
  340. position: absolute;
  341. bottom: 0;
  342. left: 0;
  343. padding: 30rpx;
  344. padding-bottom: constant(safe-area-inset-bottom);
  345. padding-bottom: env(safe-area-inset-bottom);
  346. width: 100%;
  347. background: #ffffff;
  348. box-sizing: border-box;
  349. border-radius: 30rpx 30rpx 0 0;
  350. }
  351. .audio-card {
  352. width: 100%;
  353. height: 282rpx;
  354. background: #f9f9f9;
  355. border-radius: 16rpx;
  356. margin: 0 auto;
  357. padding: 30rpx;
  358. .slider {
  359. width: 100%;
  360. margin: 0;
  361. }
  362. .slider-paly {
  363. display: flex;
  364. height: 80rpx;
  365. align-items: center;
  366. }
  367. .card-title {
  368. color: #3385ff;
  369. font-size: 28rpx;
  370. padding: 0 40rpx;
  371. text-align: center;
  372. margin-bottom: 35rpx;
  373. }
  374. .card-time {
  375. display: flex;
  376. justify-content: space-between;
  377. color: #999999;
  378. font-size: 20rpx;
  379. }
  380. .is-paly-card {
  381. width: 70rpx;
  382. height: 70rpx;
  383. flex-shrink: 0;
  384. margin-left: 30rpx;
  385. image {
  386. width: 70rpx;
  387. height: 70rpx;
  388. }
  389. }
  390. .fast-reverse {
  391. display: flex;
  392. align-items: center;
  393. justify-content: center;
  394. margin-top: 30rpx;
  395. .speed-button {
  396. width: 96rpx;
  397. height: 47rpx;
  398. background: #eaeaea;
  399. border-radius: 8rpx;
  400. text-align: center;
  401. line-height: 47rpx;
  402. margin: 0 70rpx;
  403. }
  404. .speed-img {
  405. width: 50rpx;
  406. height: 50rpx;
  407. }
  408. }
  409. }
  410. }
  411. </style>