audioBox.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="global-voice-box">
  3. <view v-if="!showBig">
  4. <view class="flex small-box" @click.prevent="showBig=true">
  5. <view style="flex:1;overflow: hidden;">
  6. <view class="van-ellipsis">{{title}}</view>
  7. <view class="time" style="font-size:24rpx;color:#666">时长 {{audioTime|formatVoiceTime}}</view>
  8. </view>
  9. <image
  10. class="pause-img"
  11. :src="play?require('@/static/audio-doing.png'):require('@/static/audio-pause-3.png')"
  12. mode="aspectFill"
  13. @click.stop="handleChangePlayStatus"
  14. />
  15. <van-icon @click.stop="handleClosePopAudio" name="cross" size="18" color="#BBC3C9"/>
  16. </view>
  17. <van-progress
  18. color="#D4AC78"
  19. track-color="#fff"
  20. :show-pivot="false"
  21. stroke-width="2px"
  22. custom-class="bot-progress"
  23. :percentage="percentage"
  24. />
  25. </view>
  26. <view class="big-box" v-else>
  27. <view class="flex top" style="overflow: hidden;">
  28. <van-icon name="arrow-down" size="18" color="#BBC3C9" @click="showBig=false" />
  29. <view class="van-ellipsis" style="flex:1;margin:0 10rpx;text-align:center">{{title}}</view>
  30. <van-icon @click.stop="handleClosePopAudio" name="cross" size="18" color="#BBC3C9"/>
  31. </view>
  32. <view class="flex center">
  33. <image
  34. class="pause-img"
  35. :src="play?require('@/static/audio-doing.png'):require('@/static/audio-pause-3.png')"
  36. mode="aspectFill"
  37. @click.stop="handleChangePlayStatus"
  38. />
  39. <text class="time">{{curTime|formatVoiceTime}}</text>
  40. <slider
  41. activeColor="#e3b377"
  42. :max="audioTime"
  43. :value="curTime"
  44. @change="handleAudioSliderChange($event)"
  45. block-size="16"
  46. class="slider"
  47. />
  48. <text class="time">{{audioTime|formatVoiceTime}}</text>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import {apiViewLogUpdate} from '@/api/common'
  55. export default {
  56. filters:{
  57. formatVoiceTime(e){
  58. let m=parseInt(e/60)
  59. let s=parseInt(e%60)
  60. return `${m>9?m:'0'+m}:${s>9?s:'0'+s}`
  61. }
  62. },
  63. computed: {
  64. audioData(){
  65. return this.$store.state.audio
  66. },
  67. //用户监听音频是否变化了 是否需要初始化
  68. audioInit(){
  69. return {
  70. reportId:this.$store.state.audio.reportId,
  71. voiceId:this.$store.state.audio.voiceId,
  72. questionId:this.$store.state.audio.questionId
  73. }
  74. },
  75. percentage(){
  76. return parseInt((this.curTime/this.audioTime)*100)
  77. },
  78. reportAudioShow(){
  79. return this.$store.state.report.audioData.show
  80. }
  81. },
  82. watch:{
  83. audioInit:{
  84. handler(nval){
  85. console.log(nval);
  86. this.init()
  87. },
  88. immediate:true
  89. },
  90. showBig:{
  91. handler(nval){
  92. this.$store.commit('audio/showBig',nval)
  93. },
  94. immediate:true
  95. }
  96. },
  97. data() {
  98. return {
  99. showBig:false,
  100. curTime:0,
  101. audioTime:0,//当前音频总时长
  102. title:'',//当前音频标题
  103. play:false
  104. }
  105. },
  106. methods: {
  107. init(){
  108. console.log('audioBox init');
  109. let delyTime=0
  110. if(this.$store.state.report.audioData.show){
  111. this.globalBgMusic.stop()
  112. delyTime=300
  113. }
  114. console.log('audioBox init',delyTime);
  115. const curAudio=this.$store.state.audio.list[this.$store.state.audio.index]
  116. setTimeout(() => {
  117. if(this.globalBgMusic.src!=curAudio.url){
  118. this.handleUpdateAudioPlayTime()
  119. this.globalBgMusic.src=curAudio.url
  120. this.globalBgMusic.title=curAudio.title
  121. }
  122. this.audioTime=curAudio.time
  123. this.title=curAudio.title
  124. this.curTime=parseInt(this.globalBgMusic.currentTime)
  125. this.play=!this.globalBgMusic.paused
  126. this.listenAudio()
  127. }, delyTime);
  128. },
  129. //音频播放事件
  130. listenAudio(){
  131. console.log('设置监听事件');
  132. this.globalBgMusic.onPlay(()=>{
  133. console.log('开始播放');
  134. this.play=true
  135. this.$store.commit('audio/updateAudioPause',false)
  136. })
  137. this.globalBgMusic.onPause(()=>{
  138. console.log('音频暂停');
  139. this.handleUpdateAudioPlayTime()
  140. this.play=false
  141. this.$store.commit('audio/updateAudioPause',true)
  142. })
  143. this.globalBgMusic.onStop(()=>{
  144. console.log('音频停止');
  145. this.handleUpdateAudioPlayTime()
  146. this.$store.commit('audio/removeAudio')
  147. })
  148. this.globalBgMusic.onEnded(()=>{
  149. console.log('音频onEnded');
  150. this.handleUpdateAudioPlayTime()
  151. const index=this.$store.state.audio.index
  152. if(index==this.$store.state.audio.list.length-1){
  153. this.$store.commit('audio/removeAudio')
  154. }else{
  155. this.handleAudioChange('next')
  156. }
  157. })
  158. this.globalBgMusic.onError((e)=>{
  159. console.log('音频onError',e);
  160. this.$store.commit('audio/removeAudio')
  161. uni.showToast({
  162. title: '音频播放错误',
  163. icon: 'none'
  164. })
  165. })
  166. this.globalBgMusic.onTimeUpdate(()=>{
  167. // console.log('时间更新');
  168. this.curTime=parseInt(this.globalBgMusic.currentTime)
  169. this.$store.commit('audio/updateAudioTime',this.curTime)
  170. })
  171. },
  172. //关闭弹窗停止播放的音频
  173. handleClosePopAudio(){
  174. this.globalBgMusic.stop()
  175. },
  176. //音频切换(暂时没有用到因为都是一个音频 如果后期有多个 直接在页面dom调用此方法即可)
  177. handleAudioChange(type){
  178. let temIndex=this.$store.state.report.audioData.index
  179. if(type=='before'){
  180. if(temIndex>0){
  181. let index=temIndex-1
  182. this.$store.commit('audio/updateAudioIndex', index)
  183. this.init()
  184. }
  185. }else{
  186. if(temIndex<this.$store.state.report.audioData.list.length-1){
  187. let index=temIndex+1
  188. this.$store.commit('audio/updateAudioIndex', index)
  189. this.init()
  190. }
  191. }
  192. },
  193. //拖动进度条
  194. handleAudioSliderChange(e){
  195. const value=e.detail.value
  196. this.globalBgMusic.seek(value)
  197. },
  198. //音频点击暂停播放
  199. handleChangePlayStatus(){
  200. if(!this.globalBgMusic.paused){
  201. this.globalBgMusic.pause()
  202. }else{
  203. this.globalBgMusic.play()
  204. }
  205. },
  206. // 记录音频播放 时长
  207. handleUpdateAudioPlayTime(){
  208. if(!this.$store.state.audio.recordId||this.$store.state.audio.curTime==0) return
  209. apiViewLogUpdate({
  210. id:this.$store.state.audio.recordId,
  211. stop_seconds:parseInt(this.$store.state.audio.curTime),
  212. source:this.$store.state.audio.lastType
  213. }).then(res=>{
  214. console.log('音频播放时间记录成功');
  215. })
  216. }
  217. },
  218. }
  219. </script>
  220. <style>
  221. .bot-progress{
  222. width: 100%;
  223. }
  224. </style>
  225. <style lang="scss" scoped>
  226. .global-voice-box{
  227. position: fixed;
  228. background: #FFFFFF;
  229. border: 1px solid #E4E4E4;
  230. box-shadow: 0px 0px 40rpx rgba(50, 35, 17, 0.25);
  231. left: 20rpx;
  232. right: 20rpx;
  233. bottom: calc(130rpx + constant(safe-area-inset-bottom));
  234. bottom: calc(130rpx + env(safe-area-inset-bottom));
  235. z-index: 99;
  236. .small-box{
  237. padding: 18rpx 48rpx 18rpx 20rpx;
  238. position: relative;
  239. align-items: center;
  240. .pause-img{
  241. width: 60rpx;
  242. height: 60rpx;
  243. margin-right: 44rpx;
  244. margin-left: 30rpx;
  245. flex-shrink: 0;
  246. }
  247. }
  248. .big-box{
  249. padding: 33rpx 30rpx;
  250. .center{
  251. align-items: center;
  252. margin-top: 20rpx;
  253. .time{
  254. font-size: 24rpx;
  255. }
  256. .pause-img{
  257. width: 60rpx;
  258. height: 60rpx;
  259. margin-right: 34rpx;
  260. }
  261. .slider{
  262. flex: 1;
  263. }
  264. }
  265. }
  266. }
  267. </style>