If you've been itching for some seriously awesome Tasker tasks (and are tired of the boring stuff like telling you how to reboot your phone or shake your phone to wake the display), then our Tasker Pro series is for you.

We will be posting a series of highly advanced Tasker profiles that will show you just how powerful Tasker can be if you are willing to think outside of the box. Over on our Tasker Tips & Tricks forums or on Reddit's /r/Tasker subreddit, you can share and work with others on how to implement an idea you might have.

This is Week 8 of Tasker pro. Last week, we showed you how to remap your volume keys to only change media volume. This week we will show you how to skip music tracks using your volume keys while the screen is off (no root required)!

One thing I would like to address from last week before we begin this week's Tasker script: many users in our comments recommended using AutoInput for a more elegant implementation. I would say that  AutoInput is a pretty good idea for that script, but I didn't recommend it because I had issues with it in the past and the required feature isn't a part of the free version of the app.


Tasker Pro #8: Volume Key Music Control

One of the most popular custom ROM features for many, many years has been the ability to control media playback using only your volume keys. The feature is so ubiquitous that I can confidently challenge our readers to find a custom ROM without this feature (compiling one yourself for this is cheating). Users without a custom ROM have turned to Xposed modules to implement this very nifty feature. But what about all of the users who haven't rooted their phones? For those users, I'm going to share a Tasker profile that will allow you to replicate this feature. Let's begin.


Requirements


Instructions

Before we begin, disable beginner mode in Tasker's preferences.

begin

Now that that's out of the way, the first thing you'll want to do is create a new profile and name it "Volume Key Music Control." You will create two contexts, one Event and one State context.

volume1

The Event context will be a Variable Set event and it should be set to detect changes in %VOLM (media volume variable). For the State context it should be Display --> Display State --> Off. This will make it so the profile will only work whenever your screen is off and the media volume changes. We will implement the actual logic to determine how to change tracks within the task, below.

 

volume2

Here is the general logic for the task. The task will check whether or not the last volume button press is within 750ms of the current volume button press. If it is, the task will then determine whether or not the current media player is Spotify (can be removed if you don't use it, or changed to another app that needs direct control) and then change tracks based on which volume button you pressed. If you didn't double click a button yet the task will instead mark the current time and save the previous media volume, in case you are in the process of double clicking.

  1. Variables --> Variable Set. Set %diffpressed to %TIMEMS - %TimePressed. Compare the timestamp when a volume button was last pressed and when the current volume button was pressed. This will serve as the threshold to determine if you meant to change tracks or just change volume.
  2. Task --> If. Set it to If %diffpressed < 750 AND %Vol neq %VOLM. If you double-pressed a volume key within 750ms AND the current saved volume does not equal the current media volume (you will see why this is important later on), then we will determine how to change the track.
  3. Plugin --> KC Tasker Processes. Make it select Spotify or another media app (YouTube, if you use YouTube Red) that you know has difficulties being controlled via traditional media button playback events. Set the variable to %spotify. (As far as I know, apps like Spotify are the exception rather than the rule, so if later on when running this task you notice it isn't changing tracks on your favorite media player, then come back to this step and add additional apps to check for).
  4. Task --> If. Set it to If %spotify ~ true.
  5. Media --> Media Control. Cmd: Next. Simulate Media Button: Yes. App: Spotify. Check If and set it to if %VOLM > %Vol. Will skip to next track if you pressed volume up twice.
  6. Media --> Media Control. Cmd: Previous. Simulate Media Button: Yes. App: Spotify. Check If and set it to if %VOLM < %Vol. Will skip to previous track if you pressed volume down twice.
  7. Audio --> Media Volume. Level: %Vol. This restores the volume level to what it previously was. Don't check anything here so the change happens silently.
  8. Task --> Else. These next few actions apply to any general media app (eg. Google Play Music)
  9. Media --> Media Control. Cmd: Next. Simulate Media Button: No. Check If and set it to if %VOLM > %VOL. Will skip to next track if you pressed volume up twice.
  10. Media --> Media Control. Cmd: Previous. Simulate Media Button: No. Check If and set it to if %VOLM < %VOL. Will skip to previous track if you pressed volume down twice.
  11. Audio --> Media Volume. Level: %Vol. This restores the volume level to what it previously was. Don't check anything here so the change happens silently.
  12. Task --> End If.
  13. Task --> Else. This will save the current time, and save the previous media volume in a variable.
  14. Variables --> Variable Set. Set %TimePressed to %TIMEMS.
  15. Variables --> Variable Set. Set %Vol to %VOLM - 1. Check if and set it to if %Vol < %VOLM.
  16. Variables --> Variable Set. Set %Vol to %VOLM + 1. Check if and set it to if %Vol > %VOLM.
  17. Task --> End If.

Notice that in actions 7 and 11 we are changing the media volume. Because of how Tasker works, this would immediately result in the task being run again (because Tasker cannot tell which source changed the media volume). That's why in action 2 we set up the condition to check if %Vol new %VOLM, which will stop this task from running if the two are equal (which they will be when this task has already run).

Let's talk about limitations in this set-up. Since this set-up requires there to be changes in the media volume, this means that if you are listening to media at maximum volume and you try to double click volume up to change tracks, it won't work.

Finally, about AutoInput. Yes, this set-up is indeed possible with AutoInput, but there are two reasons I did not recommend it. The first is that it costs money, and the second is that I couldn't get it to recognize button presses when the screen was off.


And voila! If you are able to follow along this, then congratulations, you're pretty much a master of Tasker! Confused on a step and just want to import the script and get on with your life?

If you want to import this Tasker script, you can download it from Android File Host. In order to import the profile, you need to first disable Beginner Mode in Tasker by going to the menu --> preferences. Under the UI tab, uncheck 'Beginner Mode.' Then back in the main Tasker menu, click on the 'Profiles' tab. Then long-press on the 'Profiles' tab and press 'Import.' Navigate to where you downloaded my .prf.xml file and select it to import it. Once you import it, you're free to play around with it as you see fit. This profile runs whenever your screen is off and your media volume changes twice within a 750ms time limit (and only changes tracks when you perform a double click of the same volume button).

Next week for Tasker Pro we will have a Pokemon Go-centric theme!

Check out all Tasker Pro scripts!


What would you like to see me make with Tasker? Let us know below and we might feature your idea in a future article!