Add player state

This commit is contained in:
kokarare1212 2021-02-25 18:22:28 +09:00
parent 2939ce931a
commit 7bb4f46d19
4 changed files with 34 additions and 8 deletions

4
.gitignore vendored
View File

@ -139,3 +139,7 @@ cython_debug/
# for PyCharm
.idea/
# ignore Some (Files/Directories)
credentials.json
test.py

View File

@ -6,12 +6,14 @@ from librespot.player.mixing import AudioSink
from librespot.player.playback.PlayerSession import PlayerSession
from librespot.player.state.DeviceStateHandler import DeviceStateHandler
from librespot.standard.Closeable import Closeable
import logging
import sched
import time
class Player(Closeable, PlayerSession.Listener, AudioSink.Listener):
VOLUME_MAX: int = 65536
_LOGGER: logging = logging.getLogger(__name__)
_scheduler: sched.scheduler = sched.scheduler(time.time)
_session: Session = None
_conf: PlayerConfiguration = None
@ -29,24 +31,30 @@ class Player(Closeable, PlayerSession.Listener, AudioSink.Listener):
self._events = Player.EventsDispatcher(conf)
self._sink = AudioSink(conf, self)
def init_state(self):
self._state = StateWrapper(self._session, self, self._conf)
self._init_state()
def _init_state(self):
self._state = StateWrapper.StateWrapper(self._session, self, self._conf)
class Anonymous(DeviceStateHandler.Listener):
_player: Player = None
def __init__(self, player: Player):
self._player = player
def ready(self) -> None:
pass
def command(self, endpoint: DeviceStateHandler.Endpoint, data: DeviceStateHandler.CommandBody) -> None:
pass
self._deviceStateListener = Anonymous()
self._player._LOGGER.debug("Received command: {}".format(endpoint))
self._deviceStateListener = Anonymous(self)
self._state.add_listener(self._deviceStateListener)
def volume_up(self, steps: int = 1):
if self.state is None:
if self._state is None:
return
class EventsDispatcher:
def __init__(self, conf: PlayerConfiguration):
pass

View File

@ -1,12 +1,13 @@
from __future__ import annotations
from librespot.core import Session
from librespot.dealer import DealerClient
from librespot.player import Player, PlayerConfiguration
from librespot.player.state import DeviceStateHandler
from librespot.proto import Connect
from librespot.proto.Player import ContextPlayerOptions, PlayerState, Restrictions, Suppressions
class StateWrapper(DeviceStateHandler.Listener):
class StateWrapper(DeviceStateHandler.Listener, DealerClient.MessageListener):
_state: PlayerState = None
_session: Session = None
_player: Player = None
@ -19,6 +20,9 @@ class StateWrapper(DeviceStateHandler.Listener):
self._device = DeviceStateHandler(session, self, conf)
self._state = self._init_state()
self._device.add_listener(self)
self._session.dealer().add_message_listener(self, "spotify:user:attributes:update", "hm://playlist/", "hm://collection/collection/" + self._session.username() + "/json")
def _init_state(self) -> PlayerState:
return PlayerState(
playback_speed=1.0,
@ -34,5 +38,12 @@ class StateWrapper(DeviceStateHandler.Listener):
is_playing=False
)
def add_listener(self, listener: DeviceStateHandler.Listener):
self._device.add_listener(listener)
def ready(self) -> None:
self._device.update_state(Connect.PutStateReason.NEW_DEVICE, 0, self._state)
def on_message(self, uri: str, headers: dict[str, str],
payload: bytes):
pass

View File

@ -35,6 +35,9 @@ class DeviceStateHandler:
self._LOGGER.debug("Updated Spotify-Connection-Id: {}".format(self._connectionId))
self._notify_ready()
def add_listener(self, listener: DeviceStateHandler.Listener):
self._listeners.append(listener)
def _notify_ready(self) -> None:
for listener in self._listeners:
listener.ready()