sy-audio.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <view class="audio_center">
  3. <view class="audio_center_cover">
  4. <image v-if="audioCover" class="audio_center_cover_img" mode="aspectFill" :src="audioCover"></image>
  5. <view class="audio-icon" @tap="clickAudio">
  6. <!-- <view v-if="!audio_status">点击播放</view> -->
  7. <image class="audio-img" v-if="!audio_status" src="../../static/img/audio.png"></image>
  8. <image class="audio-play" v-if="audio_status" src="../../static/img/audio_play.gif"></image>
  9. </view>
  10. </view>
  11. <view class="audio_center_right">
  12. <view v-if="stringObject(src) == 'string'" class="single">
  13. <view class="single_title">
  14. <view class="single_title_info" :style="{color:audioTitleColor}">{{audioTitle}}</view>
  15. <view class="tips">{{timeTxt}}</view>
  16. </view>
  17. <view class="tips">{{subheading}}</view>
  18. <slider :backgroundColor='backgroundColor' :activeColor='activeColor' @change="sliderChange"
  19. :value="sliderIndex" :max="maxSliderIndex" :block-size='0' />
  20. </view>
  21. <view v-else>相关功能正在开发中~</view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. let timer;
  27. export default {
  28. name: 'syAudio',
  29. emits: ['audioPlay', 'audioPause', 'audioEnd', 'audioCanplay', 'change'],
  30. data() {
  31. return {
  32. audio_status: false,
  33. timeTxt: '00 : 00',
  34. timeIndex: 0,
  35. sliderIndex: 0,
  36. maxSliderIndex: 100,
  37. stringObject: (data) => {
  38. return typeof(data)
  39. },
  40. innerAudioContext: uni.createInnerAudioContext()
  41. }
  42. },
  43. props: {
  44. //是否自动播放(只支持微信内置浏览器,小程序,app)
  45. autoplay: {
  46. type: Boolean,
  47. default: false
  48. },
  49. //音频地址
  50. src: {
  51. type: [String, Array],
  52. default: ''
  53. },
  54. //是否倒计时
  55. isCountDown: {
  56. type: Boolean,
  57. default: false
  58. },
  59. //音乐封面
  60. audioCover: {
  61. type: String,
  62. default: ''
  63. },
  64. //标题
  65. audioTitle: {
  66. type: String,
  67. default: 'new Audio Title'
  68. },
  69. //标题颜色
  70. audioTitleColor: {
  71. type: String,
  72. default: '#333'
  73. },
  74. //副标题
  75. subheading: {
  76. type: String,
  77. default: 'new Audio Subheading'
  78. },
  79. //滑块左侧已选择部分的线条颜色
  80. activeColor: {
  81. type: String,
  82. default: '#bf41a2'
  83. },
  84. //滑块右侧背景条的颜色
  85. backgroundColor: {
  86. type: String,
  87. default: '#f1c38b'
  88. }
  89. },
  90. watch: {
  91. src: {
  92. handler: function(newVal, oldVal) {
  93. this.innerAudioContext.src = typeof(newVal) == 'string' ? newVal : newVal;
  94. },
  95. deep: true
  96. }
  97. },
  98. async mounted() {
  99. this.innerAudioContext.src = typeof(this.src) == 'string' ? this.src : this.src[0];
  100. this.countDown();
  101. if (this.autoplay) {
  102. if (!this.src) return console.error('src cannot be empty,The target value is string or array')
  103. // #ifdef H5
  104. var ua = window.navigator.userAgent.toLowerCase();
  105. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  106. const jweixin = require('../../utils/jweixin');
  107. jweixin.config({});
  108. jweixin.ready(() => {
  109. WeixinJSBridge.invoke('getNetworkType', {}, (e) => {
  110. this.innerAudioContext.play();
  111. this.countDown()
  112. })
  113. })
  114. }
  115. // #endif
  116. // #ifndef H5
  117. this.innerAudioContext.autoplay = true;
  118. // #endif
  119. }
  120. this.innerAudioContext.onPlay(() => {
  121. this.audio_status = true;
  122. this.$emit('audioPlay')
  123. setTimeout(() => {
  124. this.maxSliderIndex = parseFloat(this.innerAudioContext.duration).toFixed(2);
  125. this.countDown();
  126. }, 100)
  127. });
  128. this.innerAudioContext.onPause(() => {
  129. this.$emit('audioPause');
  130. });
  131. this.innerAudioContext.onEnded(() => {
  132. this.audio_status = !this.audio_status;
  133. this.$emit('audioEnd');
  134. });
  135. this.innerAudioContext.onCanplay((event) => {
  136. this.$emit('audioCanplay');
  137. // #ifdef MP
  138. this.maxSliderIndex = parseFloat(this.innerAudioContext.duration).toFixed(2);
  139. // #endif
  140. });
  141. this.innerAudioContext.onPlay(() => {
  142. this.$emit('change', {
  143. state: true
  144. });
  145. });
  146. this.innerAudioContext.onPause(() => {
  147. this.$emit('change', {
  148. state: false
  149. });
  150. });
  151. },
  152. methods: {
  153. //销毁innerAudioContext()实例
  154. audioDestroy() {
  155. if (this.innerAudioContext) {
  156. this.innerAudioContext.destroy();
  157. this.audio_status = false;
  158. }
  159. },
  160. //跳转到指定位置
  161. audioSeek(value) {
  162. this.sliderChange(value)
  163. },
  164. //控制音乐播放/暂停
  165. audioPause() {
  166. this.clickAudio()
  167. },
  168. countDown() {
  169. timer = setInterval(() => {
  170. this.sliderIndex = parseFloat(this.innerAudioContext.currentTime).toFixed(2);
  171. this.timeTxt = this.getTime(this.isCountDown ? this.innerAudioContext.duration - this
  172. .innerAudioContext
  173. .currentTime : this.innerAudioContext.currentTime);
  174. this.timeTxt = this.isCountDown ? '- ' + this.timeTxt : this.timeTxt;
  175. if (this.innerAudioContext.currentTime >= this.innerAudioContext.duration) {
  176. clearInterval(timer);
  177. }
  178. }, 100)
  179. },
  180. clickAudio() {
  181. console.log(888,this.innerAudioContext)
  182. if (this.audio_status && !this.innerAudioContext.paused) {
  183. this.innerAudioContext.pause();
  184. clearInterval(timer);
  185. } else {
  186. this.innerAudioContext.play();
  187. }
  188. this.audio_status = !this.audio_status;
  189. },
  190. getTime(time) {
  191. let m = parseInt(time / 60 % 60)
  192. m = m < 10 ? '0' + m : m
  193. let s = parseInt(time % 60)
  194. s = s < 10 ? '0' + s : s
  195. return m + ' : ' + s
  196. },
  197. sliderChange(e) {
  198. this.innerAudioContext.seek(e.detail ? e.detail.value : e);
  199. this.sliderIndex = e.detail ? e.detail.value : e;
  200. }
  201. },
  202. onUnload() {
  203. this.audioDestroy()
  204. },
  205. onHide() {
  206. this.audioDestroy()
  207. },
  208. beforeDestroy() {
  209. this.audioDestroy()
  210. }
  211. }
  212. </script>
  213. <style lang="scss" scoped>
  214. @import url('../../static/font/iconfont.css');
  215. .icon-play{
  216. // color: #49b856 !important;
  217. }
  218. .audio-icon{
  219. width: 100%;
  220. display: flex;
  221. align-items: center;
  222. justify-content: center;
  223. // background: #49b856 !important;
  224. }
  225. .audio-img{
  226. width: 100rpx;
  227. height: 100rpx;
  228. // position: relative;
  229. // background-color: transparent;
  230. // mix-blend-mode: multiply;
  231. }
  232. .audio-play{
  233. width: 100rpx;
  234. height: 100rpx;
  235. // position: relative;
  236. // background-color: transparent;
  237. // mix-blend-mode: multiply;
  238. }
  239. .audio_center_cover{
  240. width: 100% !important;
  241. background: #fff !important;
  242. }
  243. ::v-deep uni-slider,
  244. ::v-deep slider {
  245. margin: 0;
  246. overflow: hidden;
  247. padding: 0;
  248. }
  249. .audio_center {
  250. display: flex;
  251. box-shadow: 0 0 10rpx #c3c3c3;
  252. border-radius: 10rpx;
  253. overflow: hidden;
  254. align-items: stretch;
  255. .single {
  256. flex: 1;
  257. display: flex;
  258. flex-direction: column;
  259. justify-content: space-between;
  260. padding: 16rpx 24rpx 10rpx 0;
  261. .tips {
  262. font-size: 22rpx;
  263. color: #919191;
  264. flex-shrink: 0;
  265. }
  266. &_title {
  267. &_info {
  268. font-size: 28rpx;
  269. overflow: hidden;
  270. display: -webkit-box;
  271. -webkit-line-clamp: 1;
  272. -webkit-box-orient: vertical;
  273. margin-right: 20rpx;
  274. }
  275. display: flex;
  276. justify-content: space-between;
  277. align-items: flex-end;
  278. }
  279. }
  280. &_cover {
  281. flex-shrink: 0;
  282. background: #f5f5f5;
  283. position: relative;
  284. display: flex;
  285. align-items: stretch;
  286. width: 127rpx;
  287. .iconfont {
  288. position: absolute;
  289. top: 0;
  290. left: 0;
  291. right: 0;
  292. bottom: 0;
  293. margin: auto;
  294. z-index: 9;
  295. display: flex;
  296. justify-content: center;
  297. align-items: center;
  298. font-size: 64rpx;
  299. background: rgba(0, 0, 0, .25);
  300. color: #fff;
  301. }
  302. &_img {
  303. width: 127rpx;
  304. height: 100%;
  305. border-radius: 10rpx 0 0 10rpx;
  306. }
  307. margin-right:30rpx;
  308. }
  309. &_right {
  310. display: flex;
  311. align-items: stretch;
  312. flex: 1;
  313. }
  314. }
  315. </style>