Three.js: Audio pause still not working properly

Created on 18 Feb 2020  路  4Comments  路  Source: mrdoob/three.js

Description of the problem

Hello.
When paused and resumed twice, the audio will start from the beginning.

audio.play()
...
audio.pause()
...
audio.play()
...
audio.pause()
...
audio.play() // issue occurs
Three.js version
  • [x] r113
Browser
  • [x] Chrome
  • [x] Firefox
OS
  • [x] Windows
  • [x] Android
Hardware Requirements (graphics card, VR Device, ...)
Bug

Most helpful comment

@Mugen87 I have tried your method. Each time play/pause clicked, the audio starts from right position. Thank you a lot.馃榾

All 4 comments

I override the audio pause function in my project, and it's behavior meets my expectation. The only change is variable this._pauseAt

THREE.Audio.prototype.pause = function () {
  if (this.hasPlaybackControl === false) {
    console.warn('THREE.Audio: this Audio has no playback control.')
    return
  }

  if (this.isPlaying === true) {
    // this._pausedAt = ( this.context.currentTime - this._startedAt ) * this.playbackRate;
    this._pausedAt = this.context.currentTime * this.playbackRate

    this.source.stop()
    this.source.onended = null

    this.isPlaying = false
  }

  return this
}

Yes, I can confirm that the current implementation is buggy if you start/pause the audio more than once. Thanks for the fiddles!

However, your change is not 100% correct since it does not work if you pass in a delay when first calling play().

https://jsfiddle.net/vwbnoey6/

Notice how the audio starts at the wrong place after calling pause().

I think this is the correct change: https://jsfiddle.net/8gyfszbL/

@SUDOCS Can you please verify? The idea is to implement the line like so:

this._pausedAt += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;

In this way, this._pausedAt should always point to the right position in the audio buffer. The usage of Math.max() ensures that the time value can't be negative. This could happen when using play() with a delay and immediately pause the audio.

@Mugen87 I have tried your method. Each time play/pause clicked, the audio starts from right position. Thank you a lot.馃榾

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boyravikumar picture boyravikumar  路  3Comments

danieljack picture danieljack  路  3Comments

yqrashawn picture yqrashawn  路  3Comments

seep picture seep  路  3Comments

clawconduce picture clawconduce  路  3Comments