Tasker Pro: Always Control Media Volume with Volume Keys
Automation is supposed to be about simplifying everyday tasks to give you more precious time doing the things you love. At XDA, we’ve shown you how you can use Tasker to secure your device, improve productivity, or make driving safer, all a part of our series called ‘Tasker Week.’
But the fun doesn’t end there. 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 new 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. Although we’ve done most of the hard work for you and you’re certainly free to import my profiles and use them as-is, I highly recommend you give learning how to use Tasker a shot if you want to be able to customize these tasks yourself. 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 (as I have done many times).
This is Week 7 of Tasker pro. Last week, we showed you how you can use Tasker to unlock your Windows PC using your fingerprint sensor on your Android device. This week, we will show you how to remap your volume rocker so it only controls media volume rather than ringer volume.
Tasker Pro #7: Lock your Volume Rocker
By default, the volume rockers on Android devices are set to control notification/ringer volume. Only when an application requests to play audio through the media volume channel will the media volume be used. Unfortunately, sometimes this results in odd cases where you’re trying to set the volume before starting media playback and Android just won’t let you do so when pressing the volume keys.
Many users on Reddit found this to be …. mildly infuriating. For many of these users, their primary reason for using the volume rocker is to control media volume. Why bother with having a precise way to control ringer volume when most of them toggle between vibrate and a single other volume level? This is a complaint against Android made by all kinds of users, but for most people the solutions are either not readily accessible, are half-baked and don’t work properly, or simply don’t work on their phone.
For example, custom ROM users have had the ability to control media volume with the volume rocker for years now. This feature is something you’ll find in nearly every custom ROM. But many understandably users have no desire to use a custom ROM. Rooted users can install the Xposed Framework and run a module specifically to implement this feature, but there are many phones where root access isn’t accessible. Another option is using a non-root app to remap your buttons but this does not support every device. One method that does support every device is to use the Rocker Locker app, however, this app uses a frankly terrible method to keep your volume rocker locked to media control: it actually continuously plays audio out of your speaker (seriously) which can result in huge battery drain.
Using Tasker, we can create a method that doesn’t require root, doesn’t run in the background, and works on all devices. It will prevent step changes to the ringer volume unless there is an incoming/outgoing call in progress however silent/vibrate mode will still function properly. Do note that there is a tiny delay between when the volume is changed, and even though you will see the ringer volume decrease when the volume bar shows up, Tasker will silently undo your change and apply that volume change to the media volume. This is because without root you cannot actually prevent Android from changing the ringer volume when you press the volume keys, so instead we are intercepting the volume change immediately when it occurs and redirecting it to apply to the media volume.
Disclaimer: Many variations of this set-up have existed on various Tasker forums throughout the years. It resurfaces every few months but it’s rarely seen by users outside of niche forums. Although I created this set-up from scratch, the method uses fairly simple/basic Tasker logic and so it will likely resemble the work done by others in the past.
The first thing you will need to do is create a new profile and call it something like Media Volume. The name absolutely does not matter, but pick something descriptive. We will be using two triggers* for this task, one state context and one event context. For the Event Context you will want to go to Variables and pick Variable Set. For the variable, have it monitor changes in %VOLR. Do not specify a value, as we will want Tasker to detect ANY changes to the %VOLR (Ringer Volume) dynamic global variable. For the State Context you will want to go to Phone and pick Call. Under the dropdown, select Any and then check Invert at the bottom. This will make it so the profile will only be active whenever you’re not in the middle of a call.
*Note: in order to add a second trigger to this profile, you will first have to at least add a single action to the task (or just go ahead and follow the instructions below to finish it entirely). Then, you can long-press on the first trigger you made and select the option to “Add Context.”
Next up, we will create the task itself. This is the series of actions that will run whenever Tasker detects a change in the ringer volume (the event) and that you are not in a call (the state).
- Variables –> Variable Set. Name: %VolRing to %VOLR. Check the If at the bottom and set it to if %VolRing !Set. This action is run the first time you ever run this script, and it is never run again. It runs whenever %VolRing has no value (it hasn’t been set).
- Variables –> Variable Set. Name: %volmedia to %VOLM. This will save the current media volume in a local variable.
- Task –> If. If %VOLR > %VolRing. If the current ringer volume is greater than the previous ringer volume, then this means that you’ve just pressed the volume up button.
- Variables –> Variable Set. Name: %VolRing to %VOLR-1. Check Do Maths. Set the ringer volume variable to the previous ringer volume (in essence, reverse the change).
- Variables –> Variable Set. Name: %volmedia to %VOLM+1. Check Do Maths. Increase the media volume variable (since you pressed volume up).
- Task –> Else If. If %VOLR < %VolRing. This case covers when you press volume down.
- Variables –> Variable Set. Name: %VolRing to %VOLR+1. Check Do Maths. Set the ringer volume variable to the previous ringer volume (in essence, reverse the change).
- Variables –> Variable Set. Name: %volmedia to %VOLM-1. Check Do Maths. Decrease the media volume variable (since you pressed volume down).
- Task –> Else.
- Task –> Stop. We are placing a stop action here to stop running the Task whenever the current ringer volume in %VOLR matches what is in %VolRing. We need to account for this case to prevent an infinite loop that occurs during action 13, as the second you change the ringer volume (even when it’s inside Tasker and not from a hardware button) the task will run again. But when it runs again, if you don’t have this action it will simply keep running the task. Alternatively, you can set a check in action 13 below to only run it when %VolRing =/= %VOLR.
- Task –> End If.
- Audio –> Media Volume. Press the double-arrow icon to allow you to set a variable here rather than using the slider. If you do not see this button, then you will need to disable Beginner Mode in Tasker’s preferences. For the Level set it to %volmedia. This will change the media volume.
- Audio –> Ringer Volume. Again, press the double-arrow icon to allow for variable input. For the Level set it to %VolRing. This will change the ringer volume.
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 task, 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 task runs without the need for any user input, so there is no need for you to launch the profile/task in any way.
Next week for Tasker Pro I will show you how to control your music (even Spotify) using the volume keys when the screen is off (*WITHOUT ROOT*)!
What would you like to see me make with Tasker? Let us know below and we might feature your idea in a future article!