With the development of Spotify plugin now
defunct I am using AppleScript to control Spotify.
Just simple commands to play / pause and start playlists.
however I would love to be able to query Spotifys playing state as I use both iTunes and Spotify and need to be able to pause one when the other starts to play. I can do this easily with iTunes but not Spotify.
I noticed that there is:
- Code: Select all
player state (stopped/playing/paused, r/o) : Is Spotify stopped, paused, or playing?
in the Spotify AppleScript dictionary, so I assume this is possible.
I see it done in Python from
pyspotify with the commands
PlayerState.PAUSED & PlayerState.PLAYING - Code: Select all
from __future__ import unicode_literals
import spotify
from spotify import lib
__all__ = [
'PlayerState',
]
class PlayerState(object):
UNLOADED = 'unloaded'
LOADED = 'loaded'
PLAYING = 'playing'
PAUSED = 'paused'
class Player(object):
"""Playback controller.
You'll never need to create an instance of this class yourself. You'll find
it ready to use as the :attr:`~Session.player` attribute on the
:class:`Session` instance.
"""
state = PlayerState.UNLOADED
"""The player state.
- The state is initially :attr:`PlayerState.UNLOADED`.
- When a track is loaded, the state changes to :attr:`PlayerState.LOADED`.
- When playback is started the state changes to
:attr:`PlayerState.PLAYING`.
- When playback is paused the state changes to :attr:`PlayerState.PAUSED`.
- When the track is unloaded the state changes to
:attr:`PlayerState.UNLOADED` again.
"""
def __init__(self, session):
self._session = session
def load(self, track):
"""Load :class:`Track` for playback."""
spotify.Error.maybe_raise(lib.sp_session_player_load(
self._session._sp_session, track._sp_track))
self.state = PlayerState.LOADED
def seek(self, offset):
"""Seek to the offset in ms in the currently loaded track."""
spotify.Error.maybe_raise(
lib.sp_session_player_seek(self._session._sp_session, offset))
def play(self, play=True):
"""Play the currently loaded track.
This will cause audio data to be passed to the
:attr:`~SessionCallbacks.music_delivery` callback.
If ``play`` is set to :class:`False`, playback will be paused.
"""
spotify.Error.maybe_raise(lib.sp_session_player_play(
self._session._sp_session, play))
if play:
self.state = PlayerState.PLAYING
else:
self.state = PlayerState.PAUSED
def pause(self):
"""Pause the currently loaded track.
This is the same as calling :meth:`play` with :class:`False`.
"""
self.play(False)
def unload(self):
"""Stops the currently playing track."""
spotify.Error.maybe_raise(
lib.sp_session_player_unload(self._session._sp_session))
self.state = PlayerState.UNLOADED
def prefetch(self, track):
"""Prefetch a :class:`Track` for playback.
This can be used to make libspotify download and cache a track before
playing it.
"""
spotify.Error.maybe_raise(lib.sp_session_player_prefetch(
self._session._sp_session, track._sp_track))
Any pointers on how I go about getting player state: (Playing / Paused) into a Variable in Indigo?
Thanks!