-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathheadphones_watcher.lua
47 lines (41 loc) · 1.38 KB
/
headphones_watcher.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
--- Audio-related stuff
local spotify_was_playing = false
-- Testing the new audiodevice watcher
function audiowatch(arg)
logger.df("Audiowatch arg: %s", arg)
end
hs.audiodevice.watcher.setCallback(audiowatch)
hs.audiodevice.watcher.start()
function spotify_pause()
logger.df("Pausing Spotify")
hs.spotify.pause()
end
function spotify_play()
logger.df("Playing Spotify")
hs.spotify.play()
end
-- Per-device watcher to detect headphones in/out
function audiodevwatch(dev_uid, event_name, event_scope, event_element)
logger.df("Audiodevwatch args: %s, %s, %s, %s", dev_uid, event_name, event_scope, event_element)
dev = hs.audiodevice.findDeviceByUID(dev_uid)
if event_name == 'jack' then
if dev:jackConnected() then
if spotify_was_playing then
spotify_play()
notify("Headphones plugged", "Spotify restarted")
end
else
-- Cache current state to know whether we should resume
-- when the headphones are connected again
spotify_was_playing = hs.spotify.isPlaying()
if spotify_was_playing then
spotify_pause()
notify("Headphones unplugged", "Spotify paused")
end
end
end
end
for i,dev in ipairs(hs.audiodevice.allOutputDevices()) do
dev:watcherCallback(audiodevwatch):watcherStart()
logger.df("Setting up watcher for audio device %s", dev:name())
end