index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view class="main" id="bgMusic">
  3. <view class="icon" :class="{ ran: status }" @click="musicChange">
  4. <image :src="$util.img('upload/uniapp/audio-icon-new.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: '0.4',
  14. type: [Number, String]
  15. },
  16. autoplay: {
  17. default: true,
  18. type: Boolean
  19. },
  20. musicSrc: {
  21. default: '',
  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. watch: {
  37. '$route.path'(newVal, oldVal) {
  38. if (newVal != '/promotion/pintuanfission/detail') {
  39. this.pauseTo();
  40. }
  41. }
  42. },
  43. mounted() {
  44. this.icon_style = {
  45. width: this.sizi + 'rem'
  46. // height: this.sizi + 'rem'
  47. };
  48. this.$nextTick().then(res => {
  49. const audioMusic = uni.createInnerAudioContext();
  50. audioMusic.loop = true;
  51. audioMusic.onPlay(() => {
  52. this.status = true;
  53. this.musicPlay = false;
  54. });
  55. audioMusic.onPause(() => {
  56. this.status = false;
  57. });
  58. audioMusic.onError(err => {
  59. this.musicPlay = false;
  60. console.log(err);
  61. });
  62. audioMusic.src = this.musicSrc;
  63. if (this.autoplay) audioMusic.autoplay = true;
  64. this.audioMusic = audioMusic;
  65. if (!this.autoplay) return;
  66. setTimeout(() => {
  67. if (typeof WeixinJSBridge == 'undefined') {
  68. this.playTo();
  69. } else {
  70. WeixinJSBridge.invoke('getNetworkType', {}, e => {
  71. this.playTo();
  72. });
  73. }
  74. }, 500);
  75. });
  76. },
  77. methods: {
  78. playTo() {
  79. this.audioMusic.play();
  80. },
  81. pauseTo() {
  82. this.audioMusic.pause();
  83. },
  84. musicChange() {
  85. const status = this.status;
  86. if (status) {
  87. this.audioMusic.pause();
  88. } else {
  89. this.audioMusic.play();
  90. }
  91. }
  92. },
  93. destroyed() {}
  94. };
  95. </script>
  96. <style lang="scss" scoped>
  97. .icon {
  98. position: fixed;
  99. top: 0.6rem;
  100. right: 0.2rem;
  101. z-index: 999;
  102. image {
  103. width: 0.4rem;
  104. height: 0.4rem;
  105. }
  106. }
  107. .ran {
  108. image {
  109. animation: turn 1s linear infinite;
  110. }
  111. }
  112. @keyframes turn {
  113. 0% {
  114. -webkit-transform: rotate(0deg);
  115. }
  116. 25% {
  117. -webkit-transform: rotate(90deg);
  118. }
  119. 50% {
  120. -webkit-transform: rotate(180deg);
  121. }
  122. 75% {
  123. -webkit-transform: rotate(270deg);
  124. }
  125. 100% {
  126. -webkit-transform: rotate(360deg);
  127. }
  128. }
  129. .musicPlay {
  130. position: fixed;
  131. top: 0;
  132. bottom: 0;
  133. right: 0;
  134. left: 0;
  135. background-color: rgba($color: #000000, $alpha: 0);
  136. z-index: 9999;
  137. }
  138. </style>