sound icon indicating copy to clipboard operation
sound copied to clipboard

Calling .stop() on a sound whilst the browser window is blurred does not actually stop the sound

Open alandeg opened this issue 1 year ago • 6 comments

The context of this issue is related to the auto-pause feature introduces in a recent version of this library.

Steps to reproduce:

  1. Start playing a sound
  2. Blur the window
  3. Something must stop the sound i.e. an event listener which calls .stop()
  4. Focus the window again
  5. Observe the sound continues playing when it should not

As a workaround you must disable the auto pause feature using PIXI.sound.disableAutoPause = true;

Example: https://stackblitz.com/edit/js-mhehrg?file=index.js

alandeg avatar Jan 11 '24 14:01 alandeg

Thanks for the detailed bug. That makes sense. Will look at it soon

bigtimebuddy avatar Jan 12 '24 21:01 bigtimebuddy

I'm seeing this same issue, though my repro steps are slightly simpler

  1. call pauseAll()
  2. call stop() on a specific sound instance
  3. call resumeAll()

I believe what's happening is that when pause() is called on a sound, it's _internalStop() is getting called, which is nulling this._source, so when stop() gets called while the sound is already paused, it checks if this._source exists, it doesn't, and then nothing happens. I think if we were to emit the 'stop' event any time stop() gets called, regardless of whether _source exists, Sound should trigger _onComplete() and clean up any instances still in the _instances array.

I've only looked at the v4 code, but here's the instance stop: https://github.com/pixijs/sound/blob/a84c2149e3c61fde7bec9284fdc2879a49d5230f/src/webaudio/WebAudioInstance.ts#L111-L115

and here's the _onComplete: https://github.com/pixijs/sound/blob/a84c2149e3c61fde7bec9284fdc2879a49d5230f/src/Sound.ts#L766-L776

ericente avatar May 02 '24 19:05 ericente

I'm having some issues creating a fork right now, but this snippet seems to have fixed this issue in sound v4 in my project:

import { webaudio } from '@pixi/sound';
webaudio.WebAudioInstance.prototype.stop = function(){
    if (this._source)
    {
        this._internalStop();
    }
    this.emit('stop');
}

ericente avatar May 02 '24 19:05 ericente

I'm having the same issue, is there an ETA for the fix? Version 5.2.3.

Thank you!

nicolasalt avatar Jun 06 '24 12:06 nicolasalt

I did some digging and problem seems to be lying in refreshPause calls. @nicolasalt Can you try changes from PR #270 a few times locally? It might be a candidate for fixing the issue.

CatchABus avatar Aug 21 '24 20:08 CatchABus

I've tried but your PR is on top of pixi/sound v6.0 which depends on pixijs v8, while I'm using pixijs v7.2.4.

nicolasalt avatar Aug 22 '24 11:08 nicolasalt