Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question Music Sleep Timer function: I need the timer to run out onApplicationPause.

Discussion in 'Scripting' started by MickeyJ_88, Jan 28, 2021.

  1. MickeyJ_88

    MickeyJ_88

    Joined:
    Nov 21, 2019
    Posts:
    6
    Hi All,
    I could really use some help. I'm updating an android app i've made and I want to add a sleep timer function for users to fall asleep to audio.
    I've got my audio able to run in background using Android Native Audio plugin.
    But I just can't get the timer to work out of focus or on pause. I don't need to keep track of the time I just need it to run out. I've tried a few variations of co-routines but still getting nowhere.
    Here is my working script:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class CountdownTimer : MonoBehaviour
    6. {
    7.     public float timeRemaining = 60;
    8.     public Slider slider;
    9.     public Text TimerText;
    10.     public Text RunningTimer;
    11.     public bool timerIsRunning;
    12.  
    13.  
    14.     private void Start()
    15.     {
    16.         timerIsRunning = false;
    17.     }
    18.     void Update()
    19.     {
    20.  
    21.         if (timerIsRunning)
    22.         {
    23.             slider.value = timeRemaining;
    24.             if (timeRemaining > 0)
    25.             {
    26.                 timeRemaining -= Time.deltaTime;
    27.                 DisplayTime(timeRemaining);
    28.             }
    29.             else
    30.             {
    31.                 timeRemaining = 0;
    32.                 timerIsRunning = false;
    33.                 GameObject.Find("AUDIOPANEL").GetComponent<TestAudioManager>().StopAudio();
    34.             }
    35.         }
    36.     }
    37.     //I'm not sure what to do with the update method when i'm trying to use onapplicationpause/focus....please help.
    38.     public void DisplayTime(float DisplayTime)
    39.     {
    40.         DisplayTime += 1;
    41.  
    42.         float minutes = Mathf.FloorToInt(DisplayTime / 60);
    43.         float seconds = Mathf.FloorToInt(DisplayTime % 60);
    44.  
    45.         TimerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    46.         RunningTimer.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    47.     }
    48.  
    49.     public void SetTime(float value)
    50.     {
    51.         string sliderMessage = Mathf.Round(slider.value / 60) + ":00";
    52.         TimerText.text = sliderMessage;
    53.         RunningTimer.text = sliderMessage;
    54.     }
    55.     public void StartTimer()
    56.     {
    57.         timerIsRunning = true;
    58.         timeRemaining = slider.value;
    59.     }
    60.  
    61.     public void StopTimer()
    62.     {
    63.         timerIsRunning = false;
    64.     }
    65. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Kind of like the solution above, I'm pretty sure you need to implement some sort of Android-side timer that persists and can do stuff while your app isn't in focus. I don't have direct experience with these background modes in Android but obviously they are possible.
     
    MickeyJ_88 likes this.
  3. MickeyJ_88

    MickeyJ_88

    Joined:
    Nov 21, 2019
    Posts:
    6
    I was afraid of that :( I had a hard enough time getting the Audio to work. *sigh.
    Thanks for your quick reply :)
     
  4. MickeyJ_88

    MickeyJ_88

    Joined:
    Nov 21, 2019
    Posts:
    6
    Kurt-Dekker likes this.