docs » HeadphoneAutoPause

Play/pause music players when headphones are connected/disconnected

Download: https://github.com/Hammerspoon/Spoons/raw/master/Spoons/HeadphoneAutoPause.spoon.zip

API Overview

API Documentation

Variables

autoResume
Signature HeadphoneAutoPause.autoResume
Type Variable
Description

Boolean value indicating if music should be automatically resumed when headphones are plugged in again. Only works if music was automatically paused when headphones were unplugged.

Default value: true

Source Source/HeadphoneAutoPause.spoon/init.lua
control
Signature HeadphoneAutoPause.control
Type Variable
Description

Table containing one key per application, with the value indicating whether HeadphoneAutoPause should try to pause/unpause that application in response to the headphone being plugged/unplugged. The key name must ideally correspond to the name of the corresponding hs.* module. Default value:

{
   itunes = true,
   spotify = true,
   deezer = true,
   vox = false -- Vox has built-in headphone detection support
}
Source Source/HeadphoneAutoPause.spoon/init.lua
controlfns
Signature HeadphoneAutoPause.controlfns
Type Variable
Description

Table containing control functions for each application to control. The keys must correspond to the values in HeadphoneAutoPause.control, and the value is a table with the following elements:

  • appname - application name (case-sensitive, as the application appears to the system)
  • isPlaying - function that returns a true value if the application is playing
  • play - function that starts playback in the application
  • pause - function that pauses playback in the application

The default value includes definitions for iTunes, Spotify, Deezer and Vox, using the corresponding functions from hs.itunes, hs.spotify, hs.deezer and hs.vox, respectively.

Source Source/HeadphoneAutoPause.spoon/init.lua
logger
Signature HeadphoneAutoPause.logger
Type Variable
Description

Logger object used within the Spoon. Can be accessed to set the default log level for the messages coming from the Spoon.

Source Source/HeadphoneAutoPause.spoon/init.lua

Methods

audiodevwatch
Signature HeadphoneAutoPause:audiodevwatch(dev_uid, event_name)
Type Method
Description

Callback function to use as an audio device watcher, to pause/unpause the application on headphones plugged/unplugged

Parameters:

  • dev_uid - The audio device
  • event_name - The event name
Source Source/HeadphoneAutoPause.spoon/init.lua
defaultControlFns
Signature HeadphoneAutoPause.defaultControlFns(app)
Type Method
Description

Generate the most common set of application control definition.

Parameters:

  • app - name of the application, with its correct letter casing (i.e. "iTunes"). The name as provided will be used to find the running application, and its lowercase version will be used to find the corresponding hs.* module.

Returns:

  • A table in the correct format for HeadphoneAutoPause.controlfns, using the lower-case value of app as the module name (for example, if app = "iTunes", the module loaded will be hs.itunes, and assuming the functions isPlaying(), play() and pause() exist in that module.
Source Source/HeadphoneAutoPause.spoon/init.lua
start
Signature HeadphoneAutoPause:start()
Type Method
Description

Start headphone detection on all audio devices that support it

Parameters:

  • None
Source Source/HeadphoneAutoPause.spoon/init.lua
stop
Signature HeadphoneAutoPause:stop()
Type Method
Description

Stop headphone detection

Parameters:

  • None
Source Source/HeadphoneAutoPause.spoon/init.lua