AI销管
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

431 lines
9.6 KiB

  1. <template>
  2. <view class="imt-audio">
  3. <template>
  4. <view class="top">
  5. <view class="audio-control-wrapper">
  6. <image :src="require('./static/loading.png')" v-if="playState=='loading'" class="play loading">
  7. </image>
  8. <template v-else>
  9. <image :src="require('./static/playbtn.png')" alt="play" @click="play" class="play"
  10. v-if="playState=='pause'"></image>
  11. <image :src="require('./static/pausebtn.png')" alt="pause" @click="pause" class="play" v-else>
  12. </image>
  13. </template>
  14. </view>
  15. </view>
  16. <view class="audio-wrapper">
  17. <view class="audio-flex">
  18. <text>
  19. {{formatSeconds(currentTime)}}
  20. </text>
  21. <slider class="audio-slider" block-size="12" :max="duration" :value="currentTime"
  22. @change="sliderChange" @changing="sliderChanging"></slider>
  23. <text>
  24. {{formatSeconds(duration)}}
  25. </text>
  26. </view>
  27. <view class="slidebox" @click="showTip">
  28. <slot name="extraCtrls">
  29. <image class="slide-img" :src="require('./static/backimg.png')" mode=""></image>
  30. </slot>
  31. </view>
  32. <!-- 后台播放按钮区域 -->
  33. <view class="popup" v-if="show" :class="{close: closeing}" @click="checkPlayer">
  34. <template v-if="!isBgPlay">
  35. <image :src="require('./static/bg.png')" mode=""></image>
  36. </template>
  37. <template v-else>
  38. <image :src="require('./static/bg_act.png')" mode=""></image>
  39. </template>
  40. <text :class="{'act-test': isBgPlay}">后台播放</text>
  41. </view>
  42. </view>
  43. <!--video在ios中不能完全隐藏,否则无法播放-->
  44. <video id="videoPlayer" :autoplay="false" class="videoPlayer" :src="src" :muted="false"
  45. style="width: 10rpx;height:10rpx;" @play="playerOnPlay" @pause="playerOnPause" @ended="playerOnEnded"
  46. @timeupdate="playerOnTimeupdate" @waiting="playerOnWaiting" @error="playerOnError"></video>
  47. </template>
  48. </view>
  49. </template>
  50. <script>
  51. /*
  52. createInnerAudioContext()是audio组件的内部实现,不能熄屏播放、不能后台播放、不能倍速播放。
  53. getBackgroundAudioManager() 可以熄屏播放、后台播放,不能倍速播放。缺点是响应速度很慢,无法实现精细、及时的进度控制,而且可能被别的程序占用。
  54. 因此这里只能用video来实现,video能倍速播放,不能熄屏播放、不能后台播放。而且避免了用createInnerAudioContext()实现的跳转到别的页面,还在播放的问题
  55. 因此应用程序可以在需要后台播放的时候(需要用户操作触发),再暂停这个控件的播放,然后自己用getBackgroundAudioManager实现后台播放
  56. */
  57. import Vue from 'vue';
  58. import {
  59. mapMutations
  60. } from 'vuex'
  61. import {
  62. audios
  63. } from './audioBg.js'
  64. export default {
  65. mixins: [audios],
  66. props: {
  67. nowFileTime: {
  68. type: [String, Number],
  69. default: 0
  70. },
  71. },
  72. watch: {
  73. nowFileTime(oValue, nValue) {
  74. this.duration = nValue
  75. }
  76. },
  77. data() {
  78. return {
  79. src: '', //
  80. poster: "",
  81. name: "...",
  82. singer: "...",
  83. duration: 0,
  84. currentTime: 0,
  85. playState: "pause", //"loading"/"playing"/"pause"
  86. isSliderChanging: false,
  87. isFirst: false, // 是否阻止第一次赋值
  88. show: false, // 控制展示用的
  89. closeing: false, // 默认关闭
  90. };
  91. },
  92. created() {
  93. // 自定义组件,需要传递第二个参数为this,否则后续的pause等操作不起作用
  94. this.videoCtx = uni.createVideoContext("videoPlayer", this);
  95. this.createAudio()
  96. this.setAudioFunc()
  97. },
  98. mounted() {
  99. if (this.isBgPlay) {
  100. this.backAudio()
  101. }
  102. },
  103. methods: {
  104. ...mapMutations(['createAudio', 'stopAduio']),
  105. setSrc(value) {
  106. console.log(this, ' 我打印this')
  107. this.src = value;
  108. },
  109. setPoster(value) {
  110. this.poster = value;
  111. },
  112. setName(value) {
  113. this.name = value;
  114. },
  115. setSinger(value) {
  116. this.singer = value;
  117. },
  118. playerOnPlay(e) {
  119. this.playState = "playing";
  120. console.log('playerOnPlay', e)
  121. this.$emit("play");
  122. },
  123. playerOnPause(e) {
  124. this.playState = "pause";
  125. console.log('playerOnPause', e)
  126. this.$emit("pause");
  127. },
  128. playerOnEnded(e) {
  129. this.playState = "pause";
  130. console.log('playerOnEnded', e)
  131. this.$emit("ended");
  132. },
  133. playerOnTimeupdate(e) {
  134. if (this.isFirst) this.playState = "playing";
  135. this.isFirst = true
  136. this.duration = e.detail.duration;
  137. this.currentTime = e.detail.currentTime;
  138. this.$emit("timeUpdate", e.detail);
  139. },
  140. playerOnWaiting(e) {
  141. this.playState = "loading";
  142. console.log('playerOnWaiting', e)
  143. },
  144. playerOnError(e) {
  145. console.log('playerOnError', e)
  146. this.playState = "pause";
  147. this.$emit("error", e);
  148. },
  149. formatSeconds(seconds) {
  150. var result = typeof seconds === "string" ? parseFloat(seconds) : seconds;
  151. if (isNaN(result)) return "";
  152. let h = Math.floor(result / 3600) < 10 ?
  153. "0" + Math.floor(result / 3600) :
  154. Math.floor(result / 3600);
  155. let m = Math.floor((result / 60) % 60) < 10 ?
  156. "0" + Math.floor((result / 60) % 60) :
  157. Math.floor((result / 60) % 60) + h * 60;
  158. let s = Math.floor(result % 60) < 10 ?
  159. "0" + Math.floor(result % 60) :
  160. Math.floor(result % 60);
  161. return `${h}:${m}:${s}`;
  162. },
  163. stop() {
  164. this.videoCtx.stop();
  165. },
  166. seek(t) {
  167. this.videoCtx.seek(t);
  168. },
  169. play() {
  170. // if (this.videoCtx.currentTime != this.videoCtx.currentTime) {
  171. // this.seek(this.currentTime)
  172. // }
  173. console.log('触发方法play')
  174. this.videoCtx.play(); //在有的H5浏览器里,如果play不是用户触发的,则play()会报错
  175. // 暂停后台播放
  176. this.stopAduio()
  177. },
  178. pause() {
  179. console.log('触发方法pause')
  180. this.videoCtx.pause();
  181. },
  182. playbackRate(value) {
  183. this.videoCtx.playbackRate(value);
  184. //playbackRate不能在play之前或者之后立即调用,否则只有很少几率会成功
  185. },
  186. sliderChange(e) {
  187. this.isSliderChanging = false;
  188. //要通过e.detail.value获取,否则如果通过dom去读取slider的value
  189. //就会存在滚动条拖不动的情况
  190. // this.videoCtx.seek(e.detail.value);
  191. let type = 'audio'
  192. if (this.bgAudioMannager.paused === false) {
  193. type = 'bgAudio'
  194. }
  195. this.$emit('sliderChangeComplate', { ...e, isType: type })
  196. },
  197. sliderChanging(e) {
  198. this.isSliderChanging = true;
  199. console.log(e, '当前正在改变')
  200. },
  201. // 关闭后台播放按钮
  202. closeTip() {
  203. this.closeing = false
  204. setTimeout(() => {
  205. this.show = false
  206. }, 250)
  207. },
  208. // 展示后台播放按钮
  209. showTip() {
  210. this.show = true
  211. setTimeout(() => {
  212. this.closeing = true
  213. }, 50)
  214. },
  215. // 点击后台播放音频事件
  216. backAudio() {
  217. this.pause()
  218. let obj = {
  219. src: this.src,
  220. currentTime: this.currentTime
  221. }
  222. this.setAudio(obj)
  223. },
  224. // 切换播放源
  225. checkPlayer() {
  226. this.closeTip()
  227. this.$u.debounce(() => {
  228. if (this.bgAudioMannager.paused === false) {
  229. this.$store.commit('setIsBgPlay', false)
  230. this.play()
  231. this.stopAduio()
  232. } else {
  233. this.$store.commit('setIsBgPlay', true)
  234. this.backAudio()
  235. }
  236. })
  237. }
  238. },
  239. }
  240. </script>
  241. <style lang="scss">
  242. // @import './index.scss';
  243. @mixin textoverflow() {
  244. display: -webkit-box;
  245. overflow: hidden;
  246. text-overflow: ellipsis;
  247. -webkit-box-orient: vertical;
  248. -webkit-line-clamp: 1;
  249. }
  250. @keyframes rowup {
  251. 0% {
  252. -webkit-transform: translate(-50%, -50%) rotate(0deg);
  253. transform-origin: center center;
  254. }
  255. 100% {
  256. -webkit-transform: translate(-50%, -50%) rotate(360deg);
  257. transform-origin: center center;
  258. }
  259. }
  260. .imt-audio {
  261. position: relative;
  262. width: 100%;
  263. height: 81rpx;
  264. display: flex;
  265. box-sizing: border-box;
  266. background: #fff;
  267. .top {
  268. position: relative;
  269. width: 100rpx;
  270. }
  271. .audio-wrapper {
  272. position: relative;
  273. padding: 0 20rpx;
  274. display: flex;
  275. flex: 1;
  276. color: #fff;
  277. .popup {
  278. position: absolute;
  279. right: 32rpx;
  280. top: -122rpx;
  281. z-index: 100;
  282. width: 136rpx;
  283. height: 122rpx;
  284. display: flex;
  285. align-items: center;
  286. justify-content: center;
  287. flex-direction: column;
  288. background: #fff;
  289. border: 1rpx solid #E0E0E0;
  290. transition: all 0.25s linear;
  291. opacity: 0;
  292. image {
  293. width: 32rpx;
  294. height: 32rpx;
  295. }
  296. text {
  297. margin-top: 10rpx;
  298. color: #333;
  299. font-size: 24rpx;
  300. }
  301. .act-test {
  302. color: #2671E2;
  303. }
  304. }
  305. .close {
  306. opacity: 1;
  307. }
  308. }
  309. .slidebox {
  310. flex-shrink: 0;
  311. display: flex;
  312. align-items: center;
  313. .slide-img {
  314. width: 32rpx;
  315. height: 8rpx;
  316. }
  317. }
  318. /deep/ .uni-slider-tap-area {
  319. padding: 0;
  320. }
  321. /deep/ .uni-slider-wrapper {
  322. min-height: 0;
  323. }
  324. /deep/ .uni-slider-handle-wrapper {
  325. height: 6px;
  326. }
  327. .audio-slider {
  328. flex-grow: 1;
  329. }
  330. .play {
  331. width: 48rpx;
  332. height: 48rpx;
  333. z-index: 99;
  334. background: rgba(0, 0, 0, 0.4);
  335. border-radius: 50%;
  336. position: absolute;
  337. top: 50%;
  338. left: 50%;
  339. transform: translate(-50%, -50%);
  340. &.loading {
  341. width: 48rpx;
  342. height: 48rpx;
  343. animation: rotating_theme3 2s linear infinite;
  344. }
  345. }
  346. }
  347. .audio-flex {
  348. padding: 0 32rpx 0 0;
  349. flex-grow: 1;
  350. display: flex;
  351. align-items: center;
  352. text {
  353. color: #70798D;
  354. }
  355. }
  356. @keyframes rotating {
  357. 0% {
  358. transform: rotateZ(0deg)
  359. }
  360. 100% {
  361. transform: rotateZ(360deg)
  362. }
  363. }
  364. @keyframes rotating_theme3 {
  365. 0% {
  366. transform: translate(-50%, -50%) rotateZ(0deg)
  367. }
  368. 100% {
  369. transform: translate(-50%, -50%) rotateZ(360deg)
  370. }
  371. }
  372. .hItem {
  373. margin-left: 16rpx;
  374. }
  375. .extrButton {
  376. font-size: 36rpx;
  377. }
  378. .videoPlayer {
  379. position: absolute;
  380. left: 0;
  381. bottom: 0;
  382. z-index: -1;
  383. }
  384. </style>