audioBox.vue 9.3 KB

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