Automatic Play/ Pause with AirPods
For my introductory Hackintosh build, I decided to try out some of the functions that you might find with a real Apple computer. One in particular is automatic play/pause when you connect or disconnect your AirPods. Siri works very well for this task, but it can be annoying to have to say “Hey Siri pause” before pausing or saying “Hey Siri resume” before resuming some audio. So, I came up with a script that will automatically pause audio when you connect your AirPods and automatically play if they are disconnected.
My goal was to be able to use it so that nothing needs to happen on my end. This means no voice commands or buttons need to be pressed. When you connect your AirPods, it should just work. So for this to work,
I need two parts:
Part A:- Detecting when your Airpods are connected/disconnected
Part B- Scripting the pause/play of audio using said commands
For part A, after doing some research I found out that you could listen for a specific bluetooth device to be connecting or disconnecting. This is similar to how “Hey Siri” works. The command I found was `pactl list short sinks`. To make a long story short, the command will give a list of bluetooth devices and all their characteristics. If you run it without any switches, the last column shows you when they turn on/off. If you add the -j switch, it will show seconds next to them. I chose AirPods by looking for devices with “brcmf_pcie” in their name and adding 1 second of latency because that is about how long it takes to connect to my mac.
Part B was a bit easier. The command to pause/play audio is `pactl set-sink-mute 0 false`. This works because it mutes the 1 sink connected to my monitor and then unmutes it shortly after. I added 1 second of latency before muting/unmuting because that is about how long it takes to connect to my mac.
what is airpods
I chose to use AppleScript because it already comes installed on macs. If you want to do this with a programmable keyboard, then go for it! It also happens that the script needs 2 lines of code. One line to detect if your AirPods are connected and another to pause/play the audio when they connect/disconnect. The script also needs 2 applescript files that tell it where to look for your AirPods and the command to pause/resume audio.
I made a pircord bot by using my favorite text-to-speech engine, espeak, and a programmable keyboard at the same time with applescript. I added a line in the code to say my name when I type “/name” and another line in the code to pause/play music.
methods used to achieve automatic play/pause:
There are mainly 3 methods used in this particular project, they are:
Calendar
Date & Time
Timer
how to use:
You can ask Siri to do this for you, there are two ways of doing it:
1) Start your music and then say “Hey Siri pause” or “Hey Siri play”, that’s it. It will get the job done!
2) Open settings and go to the “General” tab. Scroll down and open “Accessibility”, there you can find a setting called “Siri” where you have to turn Siri on, then just start your music and say “Hey Siri stop” or play if it’s playing already.
conclusion
This is a very simple project that I have been working on for the past few days. This method can be used to play/pause music with Airpods by using Siri, it’s very simple. I have tested this out and it works fine so far. I wanted to share this because it seems like there are not many articles about how to do something like this on iOS, hopefully this will help people.