Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

how to turn off audio on mobile

Discussion in 'Project Tiny' started by leonhandt, Apr 24, 2019.

  1. leonhandt

    leonhandt

    Joined:
    Jul 12, 2015
    Posts:
    13
    when I open the game on mobile device,then press the home button go to desktop, the audio still keeping play.
    I must need to close the game page to turn off the audio.
    how to turn off the audio and I don't need to close the game page?
     
  2. jonathangunn

    jonathangunn

    Joined:
    Apr 24, 2019
    Posts:
    2
    Try to find settings on your mobile. Can you share the screenshot and tell what device you use to play?
     
  3. leonhandt

    leonhandt

    Joined:
    Jul 12, 2015
    Posts:
    13
    I think device is not a point,because I load other web game don't have this problem. Unity sample have this problem too https://unity3d.com/tiny
     
  4. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    You could detect if the window has lost focus (which I think should happen when you press the home button), and then mute all audio in the audioConfig of Tiny.
    You would then also need to detect if the window has regained focus to resume the audio in the audioConfig.

    Something like this:
    Code (CSharp):
    1. //Not sure if "this.world" works within the inner functions,
    2. //So I store it in a variable here
    3. let world = this.world;
    4.  
    5. window.addEventListener("focus", function(evt){
    6.     let config = world.getConfigData(ut.Audio.AudioConfig);
    7.     config.paused = false;
    8.     world.setConfigData(config);
    9. }, false);
    10. window.addEventListener("blur", function(evt){
    11.     let config = world.getConfigData(ut.Audio.AudioConfig);
    12.     config.paused = true;
    13.     world.setConfigData(config);
    14. }, false);
    Haven't tested this, but you could give it a try.
     
  5. Magasenakwa

    Magasenakwa

    Joined:
    Oct 13, 2018
    Posts:
    91
    Code (csharp):
    1. public AudioSource audio;
    2. bool auto_resume = false;
    3.  
    4. void OnPause(bool paused)
    5. {
    6. if (paused)
    7. {
    8.     if (audio.isPlaying)
    9.     {
    10.         audio.Pause(); // or audio.Stop();
    11.         auto_resume = true;
    12.     }
    13. }
    14. else
    15. {
    16.     if (auto_resume)
    17.     {
    18.         auto_resume = false;
    19.         audio.Play();
    20.     }
    21. }
    22. }
    Whenever your app goes into the background Unity calls OnPause() so can't that work?
     
  6. leonhandt

    leonhandt

    Joined:
    Jul 12, 2015
    Posts:
    13
    I have tried and it not work.I press the home button still have audio.I use iphone to test.
     
  7. Odele

    Odele

    Joined:
    Jun 3, 2015
    Posts:
    8
  8. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131