audioBox.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="pop-audio-box">
  3. <view class="name">{{title}}</view>
  4. <view class="flex">
  5. <text>{{currentTime|formatVoiceTime}}</text>
  6. <slider
  7. activeColor="#e3b377"
  8. :max="audioTime"
  9. :value="currentTime"
  10. @change="handleAudioSliderChange($event)"
  11. block-size="16"
  12. class="slider"
  13. />
  14. <text>{{audioTime|formatVoiceTime}}</text>
  15. <image
  16. class="small-img"
  17. :src="play?'../static/audio-play.png':'../static/audio-pause.png'"
  18. mode="aspectFill"
  19. v-if="audioData.list.length==1"
  20. @click="handleChangePlayStatus"
  21. />
  22. </view>
  23. <view class="btn" v-if="audioData.list.length>1">
  24. <image
  25. :src="audioData.index==0?'../static/audio-change-grey.png':'../static/audio-change.png'"
  26. mode="aspectFill"
  27. class="aside"
  28. @click="handleAudioChange('before')"
  29. />
  30. <image
  31. :src="play?'../static/audio-play.png':'../static/audio-pause.png'"
  32. mode="aspectFill"
  33. class="center"
  34. @click="handleChangePlayStatus"
  35. />
  36. <image
  37. :src="audioData.index==audioData.list.length-1?'../static/audio-change-grey.png':'../static/audio-change.png'"
  38. mode="aspectFill"
  39. class="aside"
  40. style="transform: rotate(180deg)"
  41. @click="handleAudioChange('next')"
  42. />
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. computed: {
  49. audioData(){
  50. return this.$store.state.report.audioData
  51. }
  52. },
  53. watch: {
  54. 'audioData.list':{
  55. handler(nval,old){
  56. console.log('watch',nval,old);
  57. if(nval.length>0){
  58. if(old){
  59. this.init('change')
  60. }else{
  61. this.init()
  62. }
  63. }
  64. },
  65. deep:true,
  66. immediate:true
  67. }
  68. },
  69. data () {
  70. return {
  71. title:'',
  72. audioTime:0,
  73. currentTime:0,
  74. play:false
  75. }
  76. },
  77. methods: {
  78. init(type){
  79. console.log('init',this.$store.state.report.audioData);
  80. let curAudio=this.$store.state.report.audioData.list[this.$store.state.report.audioData.index]
  81. if(this.globalBgMusic.src){
  82. console.log(this.globalBgMusic);
  83. console.log(type);
  84. if(type==='change'){
  85. this.globalBgMusic.src=curAudio.video_url
  86. this.globalBgMusic.title=curAudio.video_name
  87. }else{
  88. this.currentTime=parseInt(this.globalBgMusic.currentTime)
  89. this.play=!this.globalBgMusic.paused
  90. }
  91. }else{
  92. this.globalBgMusic.src=curAudio.video_url
  93. this.globalBgMusic.title=curAudio.video_name
  94. }
  95. this.title=curAudio.video_name
  96. this.audioTime=curAudio.video_play_seconds
  97. this.handleAudioFun()
  98. },
  99. // 音频事件
  100. handleAudioFun(){
  101. this.globalBgMusic.onPlay(()=>{
  102. this.play=true
  103. this.$store.commit('updateAudioPause',false)
  104. })
  105. this.globalBgMusic.onPause(()=>{
  106. this.play=false
  107. this.$store.commit('updateAudioPause',true)
  108. })
  109. this.globalBgMusic.onStop(()=>{
  110. this.$store.commit('removeAudio')
  111. })
  112. this.globalBgMusic.onEnded(()=>{
  113. console.log('onEnded');
  114. // this.play=false
  115. let index=this.$store.state.report.audioData.index
  116. if(index==this.$store.state.report.audioData.list.length-1){
  117. this.$store.commit('removeAudio')
  118. }else{
  119. this.handleAudioChange('next')
  120. }
  121. })
  122. this.globalBgMusic.onError((e)=>{
  123. console.log('onError',e);
  124. uni.showToast({
  125. title: '音频播放错误',
  126. icon: 'none'
  127. })
  128. })
  129. this.globalBgMusic.onTimeUpdate(()=>{
  130. // console.log('时间更新');
  131. this.currentTime=parseInt(this.globalBgMusic.currentTime)
  132. })
  133. },
  134. //音频点击暂停播放
  135. handleChangePlayStatus(){
  136. if(this.play){
  137. this.globalBgMusic.pause()
  138. }else{
  139. this.globalBgMusic.play()
  140. }
  141. },
  142. //音频切换
  143. handleAudioChange(type){
  144. let temIndex=this.$store.state.report.audioData.index
  145. if(type=='before'){
  146. if(temIndex>0){
  147. let index=temIndex-1
  148. this.$store.commit('updateAudioIndex', index)
  149. this.init('change')
  150. }
  151. }else{
  152. if(temIndex<this.$store.state.report.audioData.list.length-1){
  153. let index=temIndex+1
  154. this.$store.commit('updateAudioIndex', index)
  155. this.init('change')
  156. }
  157. }
  158. },
  159. //拖动进度条
  160. handleAudioSliderChange(e){
  161. const value=e.detail.value
  162. this.globalBgMusic.seek(value)
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. .pop-audio-box{
  169. position: fixed;
  170. width: 90vw;
  171. min-height: 50rpx;
  172. left: 5vw;
  173. bottom: 30rpx;
  174. z-index: 99;
  175. background-color: #fff;
  176. border: 2px solid rgba(240, 234, 226, 0.32);
  177. border-radius: 8rpx;
  178. padding: 10rpx 15rpx;
  179. .name{
  180. text-align: center;
  181. font-size: 24rpx;
  182. }
  183. .flex{
  184. align-items: center;
  185. text{
  186. flex-shrink: 0;
  187. font-size: 20rpx;
  188. }
  189. .slider{
  190. flex: 1;
  191. margin: 0 20rpx;
  192. }
  193. .small-img{
  194. width: 36rpx;
  195. height: 36rpx;
  196. display: block;
  197. margin-left: 10rpx;
  198. flex-shrink: 0;
  199. }
  200. }
  201. .btn{
  202. display: flex;
  203. justify-content: center;
  204. align-items: center;
  205. image{
  206. display: block;
  207. }
  208. .aside{
  209. width: 36rpx;
  210. height: 36rpx;
  211. }
  212. .center{
  213. width: 44rpx;
  214. height: 44rpx;
  215. margin: 0 20rpx;
  216. }
  217. }
  218. }
  219. </style>