From f96dbf5f36b0aa7d2f97b75216a818d6b3ccc5e9 Mon Sep 17 00:00:00 2001 From: michael-grace Date: Tue, 3 Nov 2020 01:07:25 +0000 Subject: [PATCH] auto advance, repeats, play on load (not tested) --- player.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++-------- server.py | 15 +++++++++ 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/player.py b/player.py index 00134f8..7b4bd89 100644 --- a/player.py +++ b/player.py @@ -52,7 +52,9 @@ class Player(): "pos_true": 0, "remaining": 0, "length": 0, - "loop": False, + "auto_advance": True, + "repeat": "NONE", #NONE, ONE or ALL + "play_on_load": False, "output": None, "show_plan": [] } @@ -114,6 +116,8 @@ class Player(): res = json.dumps(state) return res + ### Audio Playout Related Methods + def play(self, pos=0): # if not self.isPlaying: try: @@ -172,6 +176,35 @@ class Player(): self._updateState(pos=pos) return True + def set_auto_advance(self, message: int) -> bool: + if message == 0: + self.state.update("auto_advance", False) + return True # It did it + elif message == 1: + self.state.update("auto_advance", True) + return True + else: + return False + + def set_repeat(self, message: str) -> bool: + if message in ["ALL", "ONE", "NONE"]: + self.state.update("repeat", message) + return True + else: + return False + + def set_play_on_load(self, message: int) -> bool: + if message == 0: + self.state.update("play_on_load", False) + return True # It did it + elif message == 1: + self.state.update("play_on_load", True) + return True + else: + return False + + ### Show Plan Related Methods + def add_to_plan(self, new_item: Dict[str, any]) -> bool: self.state.update("show_plan", self.state.state["show_plan"] + [PlanObject(new_item)]) return True @@ -270,6 +303,31 @@ class Player(): self.state.update("remaining", self.state.state["length"] - self.state.state["pos_true"]) + if self.state.state["remaining"] == 0: + # Track has ended + print("Finished", self.state.state["loaded_item"].name) + + # Repeat 1 + if self.state.state["repeat"] == "ONE": + self.play() + + # Auto Advance + elif self.state.state["auto_advance"]: + for i in range(len(self.state.state["show_plan"])): + if self.state.state["show_plan"][i].timeslotitemid == self.state.state["loaded_item"].timeslotitemid: + if len(self.state.state["show_plan"]) > i+1: + self.load(self.state.state["show_plan"][i+1].timeslotitemid) + break + + # Repeat All + elif self.state.state["repeat"] == "ALL": + self.load(self.state.state["show_plan"][0].timeslotitemid) + + # Play on Load + if self.state.state["play_on_load"]: + self.play() + + def _retMsg(self, msg, okay_str=False): response = self.last_msg + ":" if msg == True: @@ -339,18 +397,25 @@ class Player(): elif self.isInit: message_types: Dict[str, Callable[any, bool]] = { # TODO Check Types - "PLAY": lambda: self._retMsg(self.play()), - "PAUSE": lambda: self._retMsg(self.pause()), - "UNPAUSE": lambda: self._retMsg(self.unpause()), - "STOP": lambda: self._retMsg(self.stop()), - "SEEK": lambda: self._retMsg(self.seek(float(self.last_msg.split(":")[1]))), - "LOAD": lambda: self._retMsg(self.load(int(self.last_msg.split(":")[1]))), - "LOADED?": lambda: self._retMsg(self.isLoaded), - "UNLOAD": lambda: self._retMsg(self.unload()), - "STATUS": lambda: self._retMsg(self.status, True), - "ADD": lambda: self._retMsg(self.add_to_plan(json.loads(":".join(self.last_msg.split(":")[1:])))), - "REMOVE": lambda: self._retMsg(self.remove_from_plan(int(self.last_msg.split(":")[1]))), - "CLEAR": lambda: self._retMsg(self.clear_channel_plan()) + "STATUS": lambda: self._retMsg(self.status, True), + + # Audio Playout + "PLAY": lambda: self._retMsg(self.play()), + "PAUSE": lambda: self._retMsg(self.pause()), + "UNPAUSE": lambda: self._retMsg(self.unpause()), + "STOP": lambda: self._retMsg(self.stop()), + "SEEK": lambda: self._retMsg(self.seek(float(self.last_msg.split(":")[1]))), + "AUTOADVANCE": lambda: self._retMsg(self.set_auto_advance(int(self.last_msg.split(":")[1]))), + "REPEAT": lambda: self._retMsg(self.set_repeat(self.last_msg.split(":")[1])), + "PLAYONLOAD": lambda: self._retMsg(self.set_play_on_load(int(self.last_msg.split(":")[1]))), + + # Show Plan Items + "LOAD": lambda: self._retMsg(self.load(int(self.last_msg.split(":")[1]))), + "LOADED?": lambda: self._retMsg(self.isLoaded), + "UNLOAD": lambda: self._retMsg(self.unload()), + "ADD": lambda: self._retMsg(self.add_to_plan(json.loads(":".join(self.last_msg.split(":")[1:])))), + "REMOVE": lambda: self._retMsg(self.remove_from_plan(int(self.last_msg.split(":")[1]))), + "CLEAR": lambda: self._retMsg(self.clear_channel_plan()) } message_type: str = self.last_msg.split(":")[0] diff --git a/server.py b/server.py index 9181e66..6139b3a 100644 --- a/server.py +++ b/server.py @@ -141,6 +141,21 @@ def output(channel, name): channel_to_q[channel].put("OUTPUT:" + name) return ui_status() +@app.route("/player//autoadvance/") +def autoadvance(channel: int, state: int): + channel_to_q[channel].put("AUTOADVANCE:" + str(state)) + return ui_status() + +@app.route("/player//repeat/") +def autoadvance(channel: int, state): + channel_to_q[channel].put("REPEAT:" + state.upper()) + return ui_status() + +@app.route("/player//playonload/") +def autoadvance(channel: int, state: int): + channel_to_q[channel].put("PLAYONLOAD:" + str(state)) + return ui_status() + ### Channel Items @app.route("/player//load/")