mumu-music.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="main" id="bgMusic">
  3. <view class="icon" :class="{ran:status}" @click="musicChange">
  4. <image src="@/static/img/c-music.png" mode="widthFix" :style="icon_style"></image>
  5. </view>
  6. <view class="musicPlay" @touchstart.stop="playTo" v-if="musicPlay"></view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. sizi: {
  13. default: 60,
  14. type: [Number, String]
  15. },
  16. autoplay: {
  17. default: true,
  18. type: Boolean
  19. },
  20. musicSrc: {
  21. default: 'https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3',
  22. type: String
  23. }
  24. },
  25. data() {
  26. return {
  27. icon_style: {},
  28. audioMusic: {},
  29. status: false,
  30. musicPlay: true
  31. }
  32. },
  33. beforeDestroy() {
  34. this.pauseTo()
  35. },
  36. mounted() {
  37. this.icon_style = {
  38. width: this.sizi + 'rpx',
  39. height: this.sizi + 'rpx',
  40. }
  41. this.$nextTick()
  42. .then(res => {
  43. const audioMusic = uni.createInnerAudioContext()
  44. audioMusic.loop = true
  45. audioMusic.onPlay(() => {
  46. this.status = true
  47. this.musicPlay = false
  48. })
  49. audioMusic.onPause(() => {
  50. this.status = false
  51. })
  52. audioMusic.onError(err => {
  53. console.log(err)
  54. })
  55. audioMusic.src = this.musicSrc
  56. this.audioMusic = audioMusic
  57. if (!this.autoplay) return
  58. setTimeout(() => {
  59. if (typeof WeixinJSBridge == "undefined") return
  60. WeixinJSBridge.invoke('getNetworkType', {}, e => {
  61. this.playTo()
  62. })
  63. }, 500)
  64. })
  65. },
  66. methods: {
  67. playTo() {
  68. console.log('playTo:aaaaaaaaaaaaaaaaaaaa')
  69. this.audioMusic.play()
  70. },
  71. pauseTo() {
  72. this.audioMusic.pause()
  73. },
  74. musicChange() {
  75. const status = this.status
  76. if (status) {
  77. this.audioMusic.pause()
  78. } else {
  79. this.audioMusic.play()
  80. }
  81. }
  82. }
  83. }
  84. </script>
  85. <style lang="scss" scoped>
  86. .icon {
  87. position: fixed;
  88. top: 50rpx;
  89. right: 30rpx;
  90. z-index: 999;
  91. }
  92. .ran {
  93. image {
  94. animation: turn 1s linear infinite;
  95. }
  96. }
  97. @keyframes turn {
  98. 0% {
  99. -webkit-transform: rotate(0deg);
  100. }
  101. 25% {
  102. -webkit-transform: rotate(90deg);
  103. }
  104. 50% {
  105. -webkit-transform: rotate(180deg);
  106. }
  107. 75% {
  108. -webkit-transform: rotate(270deg);
  109. }
  110. 100% {
  111. -webkit-transform: rotate(360deg);
  112. }
  113. }
  114. .musicPlay {
  115. position: fixed;
  116. top: 0;
  117. bottom: 0;
  118. right: 0;
  119. left: 0;
  120. background-color: rgba($color: #000000, $alpha: 0);
  121. z-index: 9999;
  122. }
  123. </style>