Giter VIP home page Giter VIP logo

Comments (2)

techmasknet avatar techmasknet commented on July 24, 2024 1

时序控制上有些问题,在onPlay中又调用了一次play方法导致,创建了两个定时器。组件销毁时,只清除了一个。调整代码后,问题解决。

from vue-audio-player.

techmasknet avatar techmasknet commented on July 24, 2024

我是在组件中封装了 AudioPlayer,在退出时,确认音频播放已暂停,且在onReturn中尝试直接调用 clearTimer(),都无法销毁定时器。

<script>
import AudioPlayer from '@liripeng/vue-audio-player'
import '@liripeng/vue-audio-player/lib/vue-audio-player.css'
import { MusicService } from '../../services/music'
import { Global } from '../../techmask/utils/global'
let _ = require('lodash')
export default {
  name: 'Music',
  components: {
    AudioPlayer
  },
  props: {
    musics: {
      type: Array,
      required: true
    },
    user: {
      type: Object,
      required: true
    }
  },
  created () {
    this.currentMusic = this.musics[0]
    this.bgImg = Global.STATIC_CDN_URL + '/images/music/' + this.currentMusic.imgPath
    this.audioList.push({
      url: Global.STATIC_CDN_URL + '/media/music/' + this.currentMusic.musicCode + '.mp3'
    })
  },
  data () {
    return {
      currentMusic: null,
      useTime: 0,
      audioList: [
      ],
      requestData: {

      },
      controls: {
        showConfirmStopInd: false,
        startedInd: false
      }
    }
  },
  timers: {
    updateUseTime: { time: 1000, autostart: false, repeat: true }
  },
  methods: {
    startTreat () {
      this.useTime = 0
      this.controls.startedInd = true
      this.$timer.start('updateUseTime')
      this.$refs.audioPlayer.play()
    },
    stopTreat () {
      this.$refs.audioPlayer.pause()
      this.$timer.stop('updateUseTime')

      let requestData = this.requestData
      _.assign(requestData, _.pick(this.currentMusic, ['musicCode', 'musicName']))
      requestData.duration = this.useTime
      requestData.userId = this.user.userId

      MusicService.addMusicRecord(requestData).then(response => {
        if (Global.handleErrors(response, this)) {
          this.controls.startedInd = false
          this.onReturn()
        }
      })
    },
    updateUseTime () {
      this.useTime++
    },
    onReturn () {
      this.$refs.audioPlayer.pause()
      this.$refs.audioPlayer.clearTimer()
      alert(1)
      this.$refs.audioPlayer.isPlaying = false
      this.$emit('onReturn', {})
    },
    submit () {

    },
    onPlay () {
      if (!this.controls.startedInd) {
        this.startTreat()
      }
    },
    onEnded () {
      if (this.useTime > 60) {
        this.$refs.audioPlayer.pause()
        this.$timer.stop('updateUseTime')
        this.controls.showConfirmStopInd = true
      }
    },
    continueTimer () {
      this.$timer.start('updateUseTime')
    }
  }
}
</script>

from vue-audio-player.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.