elise-audio.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view v-if='url' class='flex audio' :class="{active:status}" :style="{width:getWith(durationS), background:audioColor}" @click='play(audioId)' >
  3. <view class='mr-3' >
  4. <view class="wifi-symbol " :class="status?'active':''">
  5. <view class="wifi-circle first"></view>
  6. <view class="wifi-circle second"></view>
  7. <view class="wifi-circle third"></view>
  8. </view>
  9. </view>
  10. <view class='ml-3'>{{durationS ? durationS + 's' : ''}}</view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. context: null,
  18. duration: 100,
  19. status: false,
  20. }
  21. },
  22. props: {
  23. url: {
  24. type:String,
  25. default:''
  26. },
  27. audioColor: {
  28. type: String,
  29. default: '#5ba5ef'
  30. },
  31. durationS:[String,Number],
  32. audioId: [String,Number]
  33. },
  34. created() {
  35. this.context = uni.createInnerAudioContext();
  36. this.context.src = this.url;
  37. this.onEnded();
  38. uni.$on('stop',(id)=> {
  39. if(id && id != this.audioId) {
  40. this.context.stop();
  41. this.status = false;
  42. } else if(!id){
  43. this.context.stop();
  44. this.status = false;
  45. }
  46. })
  47. },
  48. methods: {
  49. play(id) { //点击播放
  50. if(this.status) {
  51. this.context.pause();
  52. this.status = !this.status;
  53. }else {
  54. uni.$emit('stop',id)
  55. this.context.play()
  56. this.status = !this.status;
  57. }
  58. },
  59. onEnded() { //播放结束
  60. this.context.onEnded(()=> {
  61. this.status = false;
  62. })
  63. },
  64. getWith(s){
  65. if(s){
  66. const w = s *10 + 150
  67. return w > 520?'520rpx' :(s *10 + 150)+'rpx'
  68. }
  69. },
  70. }
  71. }
  72. </script>
  73. <style lang="scss">
  74. .audio {
  75. background: #68d7bb;
  76. height: 58rpx;
  77. border-radius: 50rpx;
  78. width: 200rpx;
  79. align-items: center;
  80. // padding: 20rpx;
  81. &.active {
  82. opacity: 0.8;
  83. }
  84. }
  85. .flex {
  86. display: flex;
  87. flex-direction: row;
  88. justify-content: space-between;
  89. }
  90. .flex-1 {
  91. flex: 1;
  92. }
  93. .ml-3 {
  94. margin-right: 30rpx;
  95. color: #fff;
  96. }
  97. .mr-3 {
  98. margin-left: 30rpx;
  99. }
  100. .wifi-symbol {
  101. width: 50rpx;
  102. height: 50rpx;
  103. box-sizing: border-box;
  104. overflow: hidden;
  105. transform: rotate(135deg) translate3d(0, 0, 0);
  106. -webkit-transform: rotate(135deg) translate3d(0, 0, 0);
  107. backface-visibility: hidden;
  108. -webkit-backface-visibility: hidden;
  109. }
  110. .wifi-circle {
  111. border: 5rpx solid #fff;
  112. border-radius: 50%;
  113. position: absolute;
  114. }
  115. .first {
  116. width: 5rpx;
  117. height: 5rpx;
  118. background: #fff;
  119. top: 45rpx;
  120. left: 45rpx;
  121. }
  122. .second {
  123. width: 25rpx;
  124. height: 25rpx;
  125. top: 35rpx;
  126. left: 35rpx;
  127. }
  128. .third {
  129. width: 40rpx;
  130. height: 40rpx;
  131. top: 25rpx;
  132. left: 25rpx;
  133. }
  134. .active {
  135. .second {
  136. animation: fadeInOut 1s infinite 0.2s;
  137. -webkit-animation: fadeInOut 1s infinite 0.2s;
  138. }
  139. .third {
  140. animation: fadeInOut 1s infinite 0.4s;
  141. -webkit-animation: fadeInOut 1s infinite 0.4s;
  142. }
  143. }
  144. @keyframes fadeInOut {
  145. 0% {
  146. opacity: 0; /*初始状态 透明度为0*/
  147. }
  148. 100% {
  149. opacity: 1; /*结尾状态 透明度为1*/
  150. }
  151. }
  152. </style>