Since the method I posted before doesn’t work on Android L 5.0 and up, I’ve edited a few steps. This fix was mentioned by Quint in the comment section. Thanks Quint!
This tutorial explains how to make your Tasker app launch Spotify when you plug in your headphones, then raise the volume and play the last played track.
What You’ll Need:
Tasker by Crafty Apps EU
We will need to setup the profile that triggers when you plug your headphones in.
• Open your Tasker application.
• Select the “Profiles” tab in the top left.
• Click the “+” icon in the bottom right.
• Click on “State“.
• Select “Hardware“.
• Select “Headset Plugged” and leave the settings as they are and press back.
Now we will setup the task (actions) that will occur when the profile is triggered.
• Press the “New Task +” that appeared.
• Let’s name it “Spotify Plug Play“.
• Click the “+” icon at the bottom to add an action.
• Select the “App” category.
• Select “Load App“. Find Spotify and select it.
• Press back.
Add a Wait action.
(This is to make sure the app is fully loaded before pressing play.)
• Click the “+” at the bottom.
• Select the “Task” category.
• Select “Wait“.
• Set Seconds to 5 or which you prefer,
Raise the Volume
• Click the “+” icon at the bottom to add another action.
• Select the “Audio” category.
• Select “Media Volume” and set Level to your desired volume.
• Press back.
If you are using Android 5.0+ and it doesn’t play, try using Play/Pause instead.
Getting Spotify to play.
• Click the “+” icon at the bottom.
• Select the “System” category.
• Select “Send Intent”
• Set these fields:
– Action: com.spotify.mobile.android.ui.widget.PLAY
– Cat: None
– Package: com.spotify.music
– Target: Broadcast Reciever
AND WE’RE DONE!
If you do not have any headphones to test this with, press the play icon down to the left.