From ba4e257fdd646b3d9125f9348aad00389af8a545 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Thu, 23 Dec 2021 19:31:36 +0000 Subject: [PATCH] Fix player bootlooping if mixer doesn't init --- player.py | 15 +++++++++++---- websocket_server.py | 22 +++++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/player.py b/player.py index b5455ff..9ce20d2 100644 --- a/player.py +++ b/player.py @@ -22,6 +22,7 @@ # Stop the Pygame Hello message. import os os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide" + from helpers.os_environment import isLinux # It's the only one we could get to work. if isLinux(): @@ -34,7 +35,7 @@ import copy import json import time from typing import Any, Callable, Dict, List, Optional -from pygame import mixer +from pygame import mixer, error from mutagen.mp3 import MP3 from syncer import sync from threading import Timer @@ -160,7 +161,7 @@ class Player: @property def status(self): - state = copy.copy(self.state.state) + state = self.state.state # Not the biggest fan of this, but maybe I'll get a better solution for this later state["loaded_item"] = ( @@ -1054,12 +1055,18 @@ class Player: self.logger.log.info( "Seeking to pos_true: " + str(loaded_state["pos_true"]) ) - self.seek(loaded_state["pos_true"]) + try: + self.seek(loaded_state["pos_true"]) + except error: + self.logger.log.error("Failed to seek on player start. Continuing anyway.") if loaded_state["playing"] is True: self.logger.log.info("Resuming playback on init.") # Use un-pause as we don't want to jump to a new position. - self.unpause() + try: + self.unpause() + except error: + self.logger.log.error("Failed to unpause on player start. Continuing anyway.") else: self.logger.log.info("No file was previously loaded to resume.") diff --git a/websocket_server.py b/websocket_server.py index bff97af..90b1af8 100644 --- a/websocket_server.py +++ b/websocket_server.py @@ -213,7 +213,21 @@ class WebsocketServer: for channel in range(len(self.webstudio_to_q)): try: message = self.webstudio_to_q[channel].get_nowait() - source = message.split(":")[0] + msg_split = message.split(":",3) + parts = len(msg_split) + source = msg_split[0] + command = msg_split[1] + if parts == 4: + #status = msg_split[2] + data = msg_split[3] + elif parts == 3: + data = msg_split[2] + else: + self.logger.log.exception( + "Invalid message size:", msg_split + ) + continue + # TODO ENUM if source not in ["WEBSOCKET", "ALL"]: self.logger.log.error( @@ -223,16 +237,14 @@ class WebsocketServer: ) continue - command = message.split(":")[1] if command == "STATUS": try: - message = message.split("OKAY:")[1] - message = json.loads(message) + message = json.loads(data) except Exception: continue # TODO more logging elif command == "POS": try: - message = message.split(":", 2)[2] + message = data except Exception: continue elif command == "QUIT":