index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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="#376cbb"
  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 ? curAudio.Name.replace(/<\/?font.*?>/g, "") : "";
  151. let activityTitle = this.$store.state.audioBg.activityTitle;
  152. this.activityTitle = activityTitle ? activityTitle.replace(/<\/?font.*?>/g, "") : "";
  153. this.play = !this.globalBgAudioManager.paused;
  154. this.listenAudio();
  155. },
  156. //音频播放事件
  157. listenAudio() {
  158. // 音频播放
  159. this.globalBgAudioManager.onPlay(async () => {
  160. if (this.isRecord) {
  161. console.log("音频播放");
  162. this.palyTime = 0;
  163. this.backAudioPlay();
  164. this.palyTimeout = setInterval(() => {
  165. this.palyTime++;
  166. }, 1000);
  167. }
  168. this.play = true;
  169. this.$store.commit("audioBg/updateAudioPause", false);
  170. this.isRecord = true;
  171. });
  172. // 音频暂停
  173. this.globalBgAudioManager.onPause(() => {
  174. console.log("音频暂停");
  175. this.backAudioPlay();
  176. clearInterval(this.palyTimeout);
  177. this.play = false;
  178. this.$store.commit("audioBg/updateAudioPause", true);
  179. });
  180. // 音频停止
  181. this.globalBgAudioManager.onStop(() => {
  182. console.log("音频停止");
  183. this.backAudioPlay();
  184. clearInterval(this.palyTimeout);
  185. this.$emit("update:showAudioPop", false);
  186. if (this.isosName == "ios") {
  187. this.play = false;
  188. this.$store.commit("audioBg/removeAudio");
  189. } else {
  190. this.play = false;
  191. this.$store.commit("audioBg/updateAudioPause", true);
  192. }
  193. });
  194. // 音频onEnded
  195. this.globalBgAudioManager.onEnded(() => {
  196. console.log("音频onEnded");
  197. this.backAudioPlay();
  198. clearInterval(this.palyTimeout);
  199. this.$emit("update:showAudioPop", false);
  200. this.$store.commit("audioBg/setAudioEnd", true);
  201. this.$store.commit("audioBg/parseIntAudio", false);
  202. this.$store.commit("audioBg/removeAudio");
  203. });
  204. // 音频onError
  205. this.globalBgAudioManager.onError((e) => {
  206. console.log("音频onError", e);
  207. this.$store.commit("audioBg/removeAudio");
  208. this.$store.commit("audioBg/setMultiple", 1);
  209. uni.showToast({
  210. title: "音频播放错误",
  211. icon: "none",
  212. });
  213. });
  214. // 音频的播放时间更新
  215. this.globalBgAudioManager.onTimeUpdate(() => {
  216. if (this.globalBgAudioManager.src && parseInt(this.globalBgAudioManager.currentTime) !== 0 && !this.isSlide) {
  217. this.curTime = parseInt(this.globalBgAudioManager.currentTime);
  218. this.$store.commit("audioBg/updateAudioTime", this.curTime);
  219. }
  220. });
  221. },
  222. // 拖动进度条
  223. handleAudioSliderChangeing(e) {
  224. this.curTime = e.detail.value;
  225. },
  226. // 拖动进度条
  227. handleAudioSliderChange(e) {
  228. const value = e.detail.value;
  229. this.globalBgAudioManager.seek(value);
  230. this.isRecord = false;
  231. setTimeout(() => {
  232. this.$store.commit("audioBg/setSlide", false);
  233. }, 300);
  234. },
  235. // 音频点击暂停播放
  236. handleChangePlayStatus() {
  237. if (!this.globalBgAudioManager.paused) {
  238. this.globalBgAudioManager.pause();
  239. } else {
  240. this.globalBgAudioManager.play();
  241. }
  242. },
  243. // 倍速播放
  244. isTimesHandler(i) {
  245. let index = i == 3 ? 0 : i + 1;
  246. this.$store.commit("audioBg/setMultiple", this.timesTheSpeed[index].value);
  247. this.globalBgAudioManager.playbackRate = this.isTimes;
  248. this.globalBgAudioManager.startTime = this.curTime;
  249. if (this.globalBgAudioManager.paused) {
  250. this.globalBgAudioManager.play();
  251. }
  252. this.isRecord = false;
  253. },
  254. //快进 快退
  255. speedReverseHandler(type) {
  256. let isTime = type == "reverse" ? this.curTime - 15 : this.curTime + 15;
  257. isTime = isTime <= 0 ? 0 : isTime >= this.audioTime ? this.audioTime - 1 : isTime;
  258. this.globalBgAudioManager.seek(isTime);
  259. this.isRecord = false;
  260. },
  261. // 播放了记录
  262. backAudioPlay() {
  263. console.log(this.$store.state.audioBg);
  264. let recordList = this.$store.state.audioBg.recordList;
  265. if ((this.$store.state.audioBg.activityId || this.$store.state.audioBg.indexId) && this.palyTime >= 0) {
  266. activity.backAudioPlay({
  267. SourceId: recordList.SourceId || recordList.ActivityId,
  268. SourceType: recordList.SourceId ? recordList.Type : 1,
  269. PlaySeconds: this.palyTime,
  270. PageRouter: this.$store.state.pageRouterReport,
  271. });
  272. } else if (this.$store.state.audioBg.reportId && this.palyTime >= 0) {
  273. Reports.reportVoiceHistoryAdd({
  274. ArticleId: this.$store.state.audioBg.reportId,
  275. PlaySeconds: this.palyTime,
  276. PageRouter: this.$store.state.pageRouterReport,
  277. });
  278. }
  279. },
  280. // 手指离开了拖动进度条
  281. touchstartHandler() {
  282. this.$store.commit("audioBg/setSlide", true);
  283. },
  284. },
  285. };
  286. </script>
  287. <style scoped lang="scss">
  288. .global-audio-box {
  289. display: flex;
  290. height: 100%;
  291. position: fixed;
  292. width: 100%;
  293. top: 0;
  294. left: 0;
  295. z-index: 111;
  296. .bg-overlay {
  297. width: 100%;
  298. height: 100%;
  299. position: absolute;
  300. top: 0;
  301. left: 0;
  302. background-color: rgba(0, 0, 0, 0.7);
  303. }
  304. .activity-title {
  305. position: relative;
  306. width: 100%;
  307. font-size: 30rpx;
  308. font-weight: 500;
  309. margin-bottom: 35rpx;
  310. padding-right: 30rpx;
  311. .icon-cross {
  312. position: absolute;
  313. right: 0;
  314. top: 50%;
  315. transform: translateY(-50%);
  316. padding: 10rpx;
  317. }
  318. }
  319. .audio-box {
  320. position: absolute;
  321. bottom: 0;
  322. left: 0;
  323. padding: 30rpx;
  324. padding-bottom: constant(safe-area-inset-bottom);
  325. padding-bottom: env(safe-area-inset-bottom);
  326. width: 100%;
  327. background: #ffffff;
  328. box-sizing: border-box;
  329. border-radius: 30rpx 30rpx 0 0;
  330. }
  331. .audio-card {
  332. width: 100%;
  333. height: 282rpx;
  334. background: #f9f9f9;
  335. border-radius: 16rpx;
  336. margin: 0 auto;
  337. padding: 30rpx;
  338. .slider {
  339. width: 100%;
  340. margin: 0;
  341. }
  342. .slider-paly {
  343. display: flex;
  344. height: 80rpx;
  345. align-items: center;
  346. }
  347. .card-title {
  348. color: #376cbb;
  349. font-size: 28rpx;
  350. padding: 0 40rpx;
  351. text-align: center;
  352. margin-bottom: 35rpx;
  353. }
  354. .card-time {
  355. display: flex;
  356. justify-content: space-between;
  357. color: #999999;
  358. font-size: 20rpx;
  359. }
  360. .is-paly-card {
  361. width: 70rpx;
  362. height: 70rpx;
  363. flex-shrink: 0;
  364. margin-left: 30rpx;
  365. image {
  366. width: 70rpx;
  367. height: 70rpx;
  368. }
  369. }
  370. .fast-reverse {
  371. display: flex;
  372. align-items: center;
  373. justify-content: center;
  374. margin-top: 30rpx;
  375. .speed-button {
  376. width: 96rpx;
  377. height: 47rpx;
  378. background: #eaeaea;
  379. border-radius: 8rpx;
  380. text-align: center;
  381. line-height: 47rpx;
  382. margin: 0 70rpx;
  383. }
  384. .speed-img {
  385. width: 50rpx;
  386. height: 50rpx;
  387. }
  388. }
  389. }
  390. }
  391. </style>