pixijs / sound

WebAudio API playback library, with filters. Modern audio playback for modern browsers.
https://pixijs.io/sound/examples/
MIT License
409 stars 67 forks source link

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

Open alandeg opened 10 months ago

alandeg commented 10 months ago

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

bigtimebuddy commented 10 months ago

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

ericente commented 7 months ago

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 commented 7 months ago

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');
}
nicolasalt commented 5 months ago

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

Thank you!

CatchABus commented 3 months ago

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.

nicolasalt commented 3 months ago

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.