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.

yz-audio.vue 7.6 KiB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view class="imt-audio">
  3. <template>
  4. <view class="top">
  5. <view class="audio-control-wrapper">
  6. <!-- <image :src="poster" mode="aspectFill" class="cover"></image> -->
  7. <image :src="require('./static/loading.png')" v-if="playState=='loading'" class="play loading">
  8. </image>
  9. <template v-else>
  10. <image :src="require('./static/playbtn.png')" alt="play" @click="play" class="play"
  11. v-if="playState=='pause'"></image>
  12. <image :src="require('./static/pausebtn.png')" alt="pause" @click="pause" class="play" v-else>
  13. </image>
  14. </template>
  15. </view>
  16. </view>
  17. <view class="audio-wrapper">
  18. <view class="audio-flex">
  19. <text>
  20. {{formatSeconds(currentTime)}}
  21. </text>
  22. <slider class="audio-slider" block-size="12" :max="duration" :value="currentTime"
  23. @change="sliderChange" @changing="sliderChanging"></slider>
  24. <text>
  25. {{formatSeconds(duration)}}
  26. </text>
  27. </view>
  28. <view class="slidebox">
  29. <slot name="extraCtrls">
  30. <text class="hItem extrButton" @click="$emit('Button1Click')"
  31. v-show="isButton1Visible">{{button1Text}}</text>
  32. <text class="hItem extrButton" @click="$emit('Button2Click')"
  33. v-show="isButton2Visible">{{button2Text}}</text>
  34. <text class="hItem extrButton" @click="$emit('Button3Click')"
  35. v-show="isButton3Visible">{{button3Text}}</text>
  36. </slot>
  37. </view>
  38. </view>
  39. <!--video在ios中不能完全隐藏,否则无法播放-->
  40. <video id="videoPlayer" class="videoPlayer" :src="src" autoplay="true" :muted="false" style="width: 10rpx;height:10rpx;"
  41. @play="playerOnPlay" @pause="playerOnPause" @ended="playerOnEnded" @timeupdate="playerOnTimeupdate"
  42. @waiting="playerOnWaiting" @error="playerOnError"></video>
  43. </template>
  44. </view>
  45. </template>
  46. <script>
  47. /*
  48. createInnerAudioContext()是audio组件的内部实现,不能熄屏播放、不能后台播放、不能倍速播放。
  49. getBackgroundAudioManager() 可以熄屏播放、后台播放,不能倍速播放。缺点是响应速度很慢,无法实现精细、及时的进度控制,而且可能被别的程序占用。
  50. 因此这里只能用video来实现,video能倍速播放,不能熄屏播放、不能后台播放。而且避免了用createInnerAudioContext()实现的跳转到别的页面,还在播放的问题
  51. 因此应用程序可以在需要后台播放的时候(需要用户操作触发),再暂停这个控件的播放,然后自己用getBackgroundAudioManager实现后台播放
  52. */
  53. import Vue from 'vue';
  54. export default {
  55. props: {
  56. isButton1Visible: {
  57. type: Boolean,
  58. default: false
  59. },
  60. button1Text: {
  61. type: String,
  62. default: ''
  63. },
  64. isButton2Visible: {
  65. type: Boolean,
  66. default: false
  67. },
  68. button2Text: {
  69. type: String,
  70. default: ''
  71. },
  72. isButton3Visible: {
  73. type: Boolean,
  74. default: false
  75. },
  76. button3Text: {
  77. type: String,
  78. default: ''
  79. },
  80. },
  81. data() {
  82. return {
  83. src: "",
  84. poster: "",
  85. name: "...",
  86. singer: "...",
  87. duration: 0,
  88. currentTime: 0,
  89. playState: "pause", //"loading"/"playing"/"pause"
  90. isSliderChanging: false,
  91. };
  92. },
  93. created: function() {
  94. //自定义组件,需要传递第二个参数为this,否则后续的pause等操作不起作用
  95. this.videoCtx = uni.createVideoContext("videoPlayer", this);
  96. },
  97. methods: {
  98. setSrc: function(value) {
  99. this.src = value;
  100. },
  101. setPoster: function(value) {
  102. this.poster = value;
  103. },
  104. setName: function(value) {
  105. this.name = value;
  106. },
  107. setSinger: function(value) {
  108. this.singer = value;
  109. },
  110. playerOnPlay: function(e) {
  111. this.playState = "playing";
  112. this.$emit("play");
  113. },
  114. playerOnPause: function(e) {
  115. this.playState = "pause";
  116. this.$emit("pause");
  117. },
  118. playerOnEnded: function(e) {
  119. this.playState = "pause";
  120. this.$emit("ended");
  121. },
  122. playerOnTimeupdate: function(e) {
  123. this.playState = "playing";
  124. this.duration = e.detail.duration;
  125. this.currentTime = e.detail.currentTime;
  126. this.$emit("timeUpdate", e.detail);
  127. },
  128. playerOnWaiting: function(e) {
  129. this.playState = "loading";
  130. },
  131. playerOnError: function(e) {
  132. uni.showToast({
  133. title: "播放出错" + e
  134. });
  135. this.playState = "pause";
  136. this.$emit("error", e);
  137. },
  138. formatSeconds(seconds) {
  139. var result = typeof seconds === "string" ? parseFloat(seconds) : seconds;
  140. if (isNaN(result)) return "";
  141. let h = Math.floor(result / 3600) < 10 ?
  142. "0" + Math.floor(result / 3600) :
  143. Math.floor(result / 3600);
  144. let m = Math.floor((result / 60) % 60) < 10 ?
  145. "0" + Math.floor((result / 60) % 60) :
  146. Math.floor((result / 60) % 60) + h * 60;
  147. let s = Math.floor(result % 60) < 10 ?
  148. "0" + Math.floor(result % 60) :
  149. Math.floor(result % 60);
  150. return `${m}:${s}`;
  151. },
  152. stop: function() {
  153. this.videoCtx.stop();
  154. },
  155. seek: function(t) {
  156. this.videoCtx.seek(t);
  157. },
  158. play: function() {
  159. var that = this;
  160. this.videoCtx.play(); //在有的H5浏览器里,如果play不是用户触发的,则play()会报错
  161. },
  162. pause: function() {
  163. this.videoCtx.pause();
  164. },
  165. playbackRate: function(value) {
  166. this.videoCtx.playbackRate(value);
  167. //playbackRate不能在play之前或者之后立即调用,否则只有很少几率会成功
  168. },
  169. sliderChange: function(e) {
  170. this.isSliderChanging = false;
  171. //要通过e.detail.value获取,否则如果通过dom去读取slider的value
  172. //就会存在滚动条拖不动的情况
  173. this.videoCtx.seek(e.detail.value);
  174. this.$emit('sliderChangeComplate', e)
  175. },
  176. sliderChanging(e) {
  177. this.isSliderChanging = true;
  178. console.log(e, '当前正在改变')
  179. },
  180. },
  181. }
  182. </script>
  183. <style lang="scss">
  184. // @import './index.scss';
  185. @mixin textoverflow() {
  186. display: -webkit-box;
  187. overflow: hidden;
  188. text-overflow: ellipsis;
  189. -webkit-box-orient: vertical;
  190. -webkit-line-clamp: 1;
  191. }
  192. @keyframes rowup {
  193. 0% {
  194. -webkit-transform: translate(-50%, -50%) rotate(0deg);
  195. transform-origin: center center;
  196. }
  197. 100% {
  198. -webkit-transform: translate(-50%, -50%) rotate(360deg);
  199. transform-origin: center center;
  200. }
  201. }
  202. .imt-audio {
  203. position: relative;
  204. width: 100%;
  205. height: 81rpx;
  206. display: flex;
  207. box-sizing: border-box;
  208. background: #fff;
  209. overflow: hidden;
  210. .top {
  211. position: relative;
  212. width: 100rpx;
  213. }
  214. .audio-wrapper {
  215. display: flex;
  216. flex-direction: column;
  217. flex: 1;
  218. color: #fff;
  219. margin-left: 20rpx;
  220. }
  221. .slidebox {
  222. display: flex;
  223. justify-content: space-between;
  224. width: 96%;
  225. }
  226. /deep/ .uni-slider-tap-area {
  227. padding: 0;
  228. }
  229. /deep/ .uni-slider-wrapper {
  230. min-height: 0;
  231. }
  232. /deep/ .uni-slider-handle-wrapper {
  233. height: 6px;
  234. }
  235. .audio-slider {
  236. flex-grow: 1;
  237. }
  238. .play {
  239. width: 48rpx;
  240. height: 48rpx;
  241. z-index: 99;
  242. background: rgba(0, 0, 0, 0.4);
  243. border-radius: 50%;
  244. position: absolute;
  245. top: 50%;
  246. left: 50%;
  247. transform: translate(-50%, -50%);
  248. &.loading {
  249. width: 48rpx;
  250. height: 48rpx;
  251. animation: rotating_theme3 2s linear infinite;
  252. }
  253. }
  254. }
  255. .audio-flex {
  256. padding: 0 32rpx 0 0;
  257. display: flex;
  258. align-items: center;
  259. text {
  260. color: #70798D;
  261. }
  262. }
  263. @keyframes rotating {
  264. 0% {
  265. transform: rotateZ(0deg)
  266. }
  267. 100% {
  268. transform: rotateZ(360deg)
  269. }
  270. }
  271. @keyframes rotating_theme3 {
  272. 0% {
  273. transform: translate(-50%, -50%) rotateZ(0deg)
  274. }
  275. 100% {
  276. transform: translate(-50%, -50%) rotateZ(360deg)
  277. }
  278. }
  279. .hItem {
  280. margin-left: 16rpx;
  281. }
  282. .extrButton {
  283. font-size: 36rpx;
  284. }
  285. .videoPlayer {
  286. position: absolute;
  287. left: 0;
  288. bottom: 0;
  289. z-index: -1;
  290. }
  291. </style>