123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <template>
- <view class="global-voice-box">
- <view v-if="!showBig">
- <view class="flex small-box" @click.prevent="showBig=true">
- <view style="flex:1;overflow: hidden;">
- <view class="van-ellipsis">{{title}}</view>
- <view class="time" style="font-size:24rpx;color:#666">时长 {{audioTime|formatVoiceTime}}</view>
- </view>
- <image
- class="pause-img"
- :src="play?require('@/static/audio-doing.png'):require('@/static/audio-pause-3.png')"
- mode="aspectFill"
- @click.stop="handleChangePlayStatus"
- />
- <van-icon @click.stop="handleClosePopAudio" name="cross" size="18" color="#BBC3C9"/>
- </view>
- <van-progress
- color="#D4AC78"
- track-color="#fff"
- :show-pivot="false"
- stroke-width="2px"
- custom-class="bot-progress"
- :percentage="percentage"
- />
- </view>
-
- <view class="big-box" v-else>
- <view class="flex top" style="overflow: hidden;">
- <van-icon name="arrow-down" size="18" color="#BBC3C9" @click="showBig=false" />
- <view class="van-ellipsis" style="flex:1;margin:0 10rpx;text-align:center">{{title}}</view>
- <van-icon @click.stop="handleClosePopAudio" name="cross" size="18" color="#BBC3C9"/>
- </view>
- <view class="flex center">
- <image
- class="pause-img"
- :src="play?require('@/static/audio-doing.png'):require('@/static/audio-pause-3.png')"
- mode="aspectFill"
- @click.stop="handleChangePlayStatus"
- />
- <text class="time">{{curTime|formatVoiceTime}}</text>
- <slider
- activeColor="#e3b377"
- :max="audioTime"
- :value="curTime"
- @change="handleAudioSliderChange($event)"
- block-size="16"
- class="slider"
- />
- <text class="time">{{audioTime|formatVoiceTime}}</text>
- </view>
- </view>
- </view>
-
- </template>
- <script>
- import {apiViewLogUpdate} from '@/api/common'
- const logger = require('@/utils/log.js')
- export default {
- filters:{
- formatVoiceTime(e){
- let m=parseInt(e/60)
- let s=parseInt(e%60)
- return `${m>9?m:'0'+m}:${s>9?s:'0'+s}`
- }
- },
- computed: {
- audioData(){
- return this.$store.state.audio
- },
- //用户监听音频是否变化了 是否需要初始化
- audioInit(){
- return {
- reportId:this.$store.state.audio.reportId,
- voiceId:this.$store.state.audio.voiceId,
- questionId:this.$store.state.audio.questionId
- }
- },
- percentage(){
- return parseInt((this.curTime/this.audioTime)*100)
- },
- reportAudioShow(){
- return this.$store.state.report.audioData.show
- }
- },
- watch:{
- audioInit:{
- handler(nval){
- console.log(nval);
- this.init()
- },
- immediate:true
- },
- showBig:{
- handler(nval){
- this.$store.commit('audio/showBig',nval)
- },
- immediate:true
- }
- },
- data() {
- return {
- showBig:false,
- curTime:0,
- audioTime:0,//当前音频总时长
- title:'',//当前音频标题
- play:false
- }
- },
- methods: {
- init(){
- console.log('audioBox init');
- let delyTime=0
- if(this.$store.state.report.audioData.show){
- this.globalBgMusic.stop()
- delyTime=300
- }
- console.log('audioBox init',delyTime);
- const curAudio=this.$store.state.audio.list[this.$store.state.audio.index]
- setTimeout(() => {
- if(this.globalBgMusic.src!=curAudio.url){
- this.handleUpdateAudioPlayTime()
- this.globalBgMusic.src=curAudio.url
- this.globalBgMusic.title=curAudio.title
- this.globalBgMusic.startTime=this.$store.state.audio.startTime
- }
- this.audioTime=curAudio.time
- this.title=curAudio.title
- this.curTime=parseInt(this.globalBgMusic.currentTime)
- this.play=!this.globalBgMusic.paused
- this.listenAudio()
- }, delyTime);
- },
- //音频播放事件
- listenAudio(){
- console.log('设置监听事件');
- this.globalBgMusic.onPlay(()=>{
- console.log('开始播放');
- this.play=true
- this.$store.commit('audio/updateAudioPause',false)
- })
- this.globalBgMusic.onPause(()=>{
- console.log('音频暂停');
- this.handleUpdateAudioPlayTime()
- this.play=false
- this.$store.commit('audio/updateAudioPause',true)
- })
- this.globalBgMusic.onStop(()=>{
- console.log('音频停止');
- this.handleUpdateAudioPlayTime()
- this.$store.commit('audio/removeAudio')
- })
- this.globalBgMusic.onEnded(()=>{
- console.log('音频onEnded');
- this.handleUpdateAudioPlayTime()
- const index=this.$store.state.audio.index
- if(index==this.$store.state.audio.list.length-1){
- this.$store.commit('audio/removeAudio')
- }else{
- this.handleAudioChange('next')
- }
-
- })
- this.globalBgMusic.onError((e)=>{
- console.log('音频onError',e);
- logger.error('音频onError',e)
- this.$store.commit('audio/removeAudio')
- uni.showToast({
- title: '音频播放错误',
- icon: 'none'
- })
- })
- this.globalBgMusic.onTimeUpdate(()=>{
- // console.log('时间更新');
- this.curTime=parseInt(this.globalBgMusic.currentTime)
- this.$store.commit('audio/updateAudioTime',this.curTime)
- })
- },
- //关闭弹窗停止播放的音频
- handleClosePopAudio(){
- this.globalBgMusic.stop()
- },
- //音频切换(暂时没有用到因为都是一个音频 如果后期有多个 直接在页面dom调用此方法即可)
- handleAudioChange(type){
- let temIndex=this.$store.state.report.audioData.index
- if(type=='before'){
- if(temIndex>0){
- let index=temIndex-1
- this.$store.commit('audio/updateAudioIndex', index)
- this.init()
- }
- }else{
- if(temIndex<this.$store.state.report.audioData.list.length-1){
- let index=temIndex+1
- this.$store.commit('audio/updateAudioIndex', index)
- this.init()
- }
- }
- },
- //拖动进度条
- handleAudioSliderChange(e){
- const value=e.detail.value
- this.globalBgMusic.seek(value)
- },
- //音频点击暂停播放
- handleChangePlayStatus(){
- if(!this.globalBgMusic.paused){
- this.globalBgMusic.pause()
- }else{
- this.globalBgMusic.play()
- }
- },
- // 记录音频播放 时长
- handleUpdateAudioPlayTime(){
- if(!this.$store.state.audio.recordId||this.$store.state.audio.curTime==0) return
- apiViewLogUpdate({
- id:this.$store.state.audio.recordId,
- stop_seconds:parseInt(this.$store.state.audio.curTime),
- source:this.$store.state.audio.lastType
- }).then(res=>{
- console.log('音频播放时间记录成功');
- })
- }
- },
- }
- </script>
- <style>
- .bot-progress{
- width: 100%;
- }
- </style>
- <style lang="scss" scoped>
- .global-voice-box{
- position: fixed;
- background: #FFFFFF;
- border: 1px solid #E4E4E4;
- box-shadow: 0px 0px 40rpx rgba(50, 35, 17, 0.25);
- left: 20rpx;
- right: 20rpx;
- bottom: calc(130rpx + constant(safe-area-inset-bottom));
- bottom: calc(130rpx + env(safe-area-inset-bottom));
- z-index: 99;
- .small-box{
- padding: 18rpx 48rpx 18rpx 20rpx;
- position: relative;
- align-items: center;
- .pause-img{
- width: 60rpx;
- height: 60rpx;
- margin-right: 44rpx;
- margin-left: 30rpx;
- flex-shrink: 0;
- }
- }
- .big-box{
- padding: 33rpx 30rpx;
- .center{
- align-items: center;
- margin-top: 20rpx;
- .time{
- font-size: 24rpx;
- }
- .pause-img{
- width: 60rpx;
- height: 60rpx;
- margin-right: 34rpx;
- }
- .slider{
- flex: 1;
- }
- }
- }
- }
- </style>
|