• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!
  • Fill out your device list and let everyone know which phones you have!    Edit Your Device Inventory

[GUIDE][Difficulty: Advanced] Custom Sleep Timer for any Audio App

Search This thread

MishaalRahman

Editor in Chief
Nov 2, 2015
1,040
2,128
www.xda-developers.com
Sleep timers are a pretty crucial feature for anybody who likes to listen to music, podcasts, or other audio at night. Using Tasker, we can make our own custom sleep timer.

Disclaimer: this was created by Rachet_Guy over on /r/Tasker. Credits to him for putting this together!

Instructions

  1. Create a new profile and name it "Sleep Timer".
  2. Add a Time context, and set it to every 2 minutes (or however long you want it to run before the volume decreases).
    q0IlGqA.jpg
  3. Create a new task, and name it "Sleep Timer".
  4. Variables --> Variable Set. Name %media_start_vol and set it to a value you feel comfortable with.
  5. Variables --> Variable Set. Name is %vol_decr and set it to a decrement value. NOTE: you must pick a starting volume and/or a decrement volume such that when you subtract the two eventually the value should equal 0, otherwise Tasker will not be able to set the final volume level and will fail.
  6. Task --> If. If %SleepTimerRunning != 1 AND %PENABLED !~ *Sleep Timer. This is basically checking where I'm at. If I haven't started the sleep timer yet, then we'll start it from the beginning.
  7. Alert --> Flash. Text: SLEEP TIMER - CYCLE BEGIN.
  8. Audio --> Media Volume. Set Level to %media_start_vol.
  9. OPTIONAL: if your music player supports an intent broadcast or has a plugin to start audio playback, then you can create an action to start it here. Otherwise, start audio playback before running the task.
  10. Tasker --> Profile Status. Name: Sleep Timer. Set: On.
  11. Task --> Wait. Wait 1 second.
  12. Variables --> Variable Set. Name %SleepTimerRunning To 1.
  13. OPTIONAL: Plugin --> Zooper Widget Variable. Configuration: #TSLEEPTIMER# = 1. This is if you want a Zooper widget that shows when the sleep timer is on.
  14. Task --> Else If. If %SleepTimerRunning = 1 AND %PENABLED ~ *Sleep Timer* AND %VOLM > 0. This is checking to see if the sleep timer is still active and if the media volume is still active.
  15. Variables --> Variable Set. Name %curr_vol to %VOLM. Set the current volume variable to the media volume.
  16. Variables --> Variable Set. Name %next_media_vol to %curr_vol-%vol_decr. We're decrementing the current volume.
  17. Audio --> Media Volume. Level: %next_media_vol. Changing the media volume.
  18. Alert --> Flash. Text: VOLUME LOWERED TO - %next_media_vol
  19. Task --> Else.
  20. Tasker --> Profile Status. Name: Sleep Timer, Set Off.
  21. Task --> Wait. Wait 2 seconds.
  22. Media --> Media Control. Cmd: Toggle Pause. Simulate Media Button: On. For App: choose the music app you want it to broadcast to. Check If and set it to If %SleepTimerRunning eq 1.
  23. Variables --> Variable Set. Set %SleepTimerRunning to 0.
  24. OPTIONAL: Plugin --> Zooper Widget Pro Variable. Configuration: #TSLEEPTIMER# = 0. We're toggling the Zooper Widget to show us that it's off.
  25. Audio --> Media Volume. Level: %media_start_vol
  26. Alert --> Flash. Text: SLEEP TIMER - CYCLE END
  27. Task --> End If.
  28. Exit out, and the profile creation is done.

Now, TURN OFF THE PROFILE! You don't want it running unless you explicitly tell it to! You can run it in many different ways, me personally I use a Zooper Widget to make it run, but you can simply create a homescreen shortcut using Nova or Apex Launcher to launch the Sleep Timer task.

Here are screenshots of the task:
kBQRfGz.jpg

2cb3By4.jpg

SpDGTj7.jpg

mrCEC8r.jpg


I'll attach the XML file below. Import it by moving it to /sdcard/Tasker/Profiles, then opening Tasker and long-pressing on the Profile tab up top then pressing Import.
 

Attachments

  • Sleep_Timer.prf.xml
    8.6 KB · Views: 665

Top Liked Posts

  • There are no posts matching your filters.
  • 3
    Sleep timers are a pretty crucial feature for anybody who likes to listen to music, podcasts, or other audio at night. Using Tasker, we can make our own custom sleep timer.

    Disclaimer: this was created by Rachet_Guy over on /r/Tasker. Credits to him for putting this together!

    Instructions

    1. Create a new profile and name it "Sleep Timer".
    2. Add a Time context, and set it to every 2 minutes (or however long you want it to run before the volume decreases).
      q0IlGqA.jpg
    3. Create a new task, and name it "Sleep Timer".
    4. Variables --> Variable Set. Name %media_start_vol and set it to a value you feel comfortable with.
    5. Variables --> Variable Set. Name is %vol_decr and set it to a decrement value. NOTE: you must pick a starting volume and/or a decrement volume such that when you subtract the two eventually the value should equal 0, otherwise Tasker will not be able to set the final volume level and will fail.
    6. Task --> If. If %SleepTimerRunning != 1 AND %PENABLED !~ *Sleep Timer. This is basically checking where I'm at. If I haven't started the sleep timer yet, then we'll start it from the beginning.
    7. Alert --> Flash. Text: SLEEP TIMER - CYCLE BEGIN.
    8. Audio --> Media Volume. Set Level to %media_start_vol.
    9. OPTIONAL: if your music player supports an intent broadcast or has a plugin to start audio playback, then you can create an action to start it here. Otherwise, start audio playback before running the task.
    10. Tasker --> Profile Status. Name: Sleep Timer. Set: On.
    11. Task --> Wait. Wait 1 second.
    12. Variables --> Variable Set. Name %SleepTimerRunning To 1.
    13. OPTIONAL: Plugin --> Zooper Widget Variable. Configuration: #TSLEEPTIMER# = 1. This is if you want a Zooper widget that shows when the sleep timer is on.
    14. Task --> Else If. If %SleepTimerRunning = 1 AND %PENABLED ~ *Sleep Timer* AND %VOLM > 0. This is checking to see if the sleep timer is still active and if the media volume is still active.
    15. Variables --> Variable Set. Name %curr_vol to %VOLM. Set the current volume variable to the media volume.
    16. Variables --> Variable Set. Name %next_media_vol to %curr_vol-%vol_decr. We're decrementing the current volume.
    17. Audio --> Media Volume. Level: %next_media_vol. Changing the media volume.
    18. Alert --> Flash. Text: VOLUME LOWERED TO - %next_media_vol
    19. Task --> Else.
    20. Tasker --> Profile Status. Name: Sleep Timer, Set Off.
    21. Task --> Wait. Wait 2 seconds.
    22. Media --> Media Control. Cmd: Toggle Pause. Simulate Media Button: On. For App: choose the music app you want it to broadcast to. Check If and set it to If %SleepTimerRunning eq 1.
    23. Variables --> Variable Set. Set %SleepTimerRunning to 0.
    24. OPTIONAL: Plugin --> Zooper Widget Pro Variable. Configuration: #TSLEEPTIMER# = 0. We're toggling the Zooper Widget to show us that it's off.
    25. Audio --> Media Volume. Level: %media_start_vol
    26. Alert --> Flash. Text: SLEEP TIMER - CYCLE END
    27. Task --> End If.
    28. Exit out, and the profile creation is done.

    Now, TURN OFF THE PROFILE! You don't want it running unless you explicitly tell it to! You can run it in many different ways, me personally I use a Zooper Widget to make it run, but you can simply create a homescreen shortcut using Nova or Apex Launcher to launch the Sleep Timer task.

    Here are screenshots of the task:
    kBQRfGz.jpg

    2cb3By4.jpg

    SpDGTj7.jpg

    mrCEC8r.jpg


    I'll attach the XML file below. Import it by moving it to /sdcard/Tasker/Profiles, then opening Tasker and long-pressing on the Profile tab up top then pressing Import.
    2
    I'd change it so that it would PAUSE ( + Close + Disconnect any bluetooth speakers ) the media app