Search Unity

Stop everything in x seconds (running in background)

Discussion in 'Scripting' started by Simon75012, Feb 19, 2019.

  1. Simon75012

    Simon75012

    Joined:
    Sep 15, 2016
    Posts:
    79
    Hi,
    I have an app playing sounds when app running in background (ios and android).

    I'd like to force stop the sounds X minutes after the app enter background mode.

    I have no control at all once the app enter the background mode. I'm not sure if this kind of action is doable...

    Thanks!
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. Simon75012

    Simon75012

    Joined:
    Sep 15, 2016
    Posts:
    79
    Is this work for ios and android or only standalone platform?

    I just tried :
    void OnApplicationFocus(bool isFocus){
    if(!isFocus) { //put app in background
    StartCoroutine(ieStopSoundIn(10f));
    }
    }
    IEnumerator ieStopSoundIn ( float f ) {
    yield return new WaitForSeconds(f);
    PauseSound();
    }

    But i don't think the coroutine work once the app is in background.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It should work similar for all platforms. Is the game object active?
     
  5. Simon75012

    Simon75012

    Joined:
    Sep 15, 2016
    Posts:
    79
    Yes it is.
    Does the code above should work?
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  7. Simon75012

    Simon75012

    Joined:
    Sep 15, 2016
    Posts:
    79
    So, is there a way to force stop the sounds X minutes after the app enter background mode? For ios and Android.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    There is but it is outside of Unity in the sense that it is either a native ObjectionableC plugin for iOS or a Java call for Android.

    Both of those things are usable inside of Unity with standard C# interop, but are technically outside of Unity.

    In addition to having the correct native code snippets above, each platform (iOS and/or Android) requires you to assert either entitlements or permissions to continue running in the background, and further will require an additional review from Apple or Google, including (possibly) an explanation of why your app needs this and why it can't be done another way.

    Here's the iOS page: https://developer.apple.com/library.../BackgroundExecution/BackgroundExecution.html

    Here's the Android page on it: https://developer.android.com/guide/background/

    Good luck!