index.vue 11 KB

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