Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

[BUG] AudioConfig is not paused when hide the game window

Discussion in 'Project Tiny' started by andrewmil, Feb 7, 2019.

  1. andrewmil

    andrewmil

    Joined:
    Dec 30, 2018
    Posts:
    10
    When i hide browser window (chrome/safari/desktop/mobile), in a debugger i see -

    ["visibilitychange ","hidden"]
    ["audioConf.paused ",true]

    but background music still playing.

    Code (JavaScript):
    1.  
    2. document.addEventListener( 'visibilitychange', function (): void {
    3. if (document.visibilityState == "hidden") {
    4.                     console.log("visibilitychange ", document.visibilityState);
    5.                     let audioConf = world.getConfigData( ut.Audio.AudioConfig );
    6.                     audioConf.paused = true;
    7.                     world.setConfigData( audioConf );
    8.  
    9.                     audioConf = world.getConfigData(u t.Audio.AudioConfig) ;
    10.                     console.log("audioConf.paused ", audioConf.paused);
    11.                 }
    12. });
    13.  
     
  2. Deleted User

    Deleted User

    Guest

    I'm using this piece of code, isn't the best solution, but works for me

    Code (JavaScript):
    1. const _out = () => {
    2.     const audioConfig = world.getConfigData(ut.Audio.AudioConfig)
    3.     audioConfig.paused = true
    4.     world.setConfigData(audioConfig)
    5. }
    6.  
    7. const _in = () => {
    8.     const audioConfig = world.getConfigData(ut.Audio.AudioConfig)
    9.     audioConfig.paused = false
    10.     world.setConfigData(audioConfig)
    11. }
    12.  
    13. const canvas = document.getElementById('UT_CANVAS')
    14.  
    15. canvas.addEventListener('blur', _out)
    16. canvas.addEventListener('mouseout', _out)
    17.  
    18. canvas.addEventListener('focus', _in)
    19. canvas.addEventListener('click', _in)
    20. canvas.addEventListener('mousein', _in)
     
    alamac123 and andrewmil like this.