feat: Remove load_main_playlist endpoint and implement automatic loading on startup

This commit is contained in:
JSC
2025-07-07 21:34:29 +02:00
parent bcd6ca8104
commit 96ab2bdf77
2 changed files with 25 additions and 37 deletions

View File

@@ -219,18 +219,6 @@ def stop_vlc_instance():
return ErrorHandlingService.handle_generic_error(e) return ErrorHandlingService.handle_generic_error(e)
@bp.route("/load-main-playlist", methods=["POST"])
@require_auth
def load_main_playlist():
"""Load the main playlist into the player."""
try:
success = music_player_service.load_main_playlist()
if success:
return jsonify({"message": "Main playlist loaded successfully"}), 200
return jsonify({"error": "Failed to load main playlist"}), 400
except Exception as e:
return ErrorHandlingService.handle_generic_error(e)
@bp.route("/test-emit", methods=["POST"]) @bp.route("/test-emit", methods=["POST"])
@require_auth @require_auth

View File

@@ -23,7 +23,7 @@ class MusicPlayerService:
"""Initialize the music player service.""" """Initialize the music player service."""
self.instance: Optional[vlc.Instance] = None self.instance: Optional[vlc.Instance] = None
self.player: Optional[vlc.MediaPlayer] = None self.player: Optional[vlc.MediaPlayer] = None
self.app = None # Store Flask app instance for context self.app: Optional[Any] = None # Store Flask app instance for context
self.current_playlist_id: Optional[int] = None self.current_playlist_id: Optional[int] = None
self.current_track_index = 0 self.current_track_index = 0
@@ -67,6 +67,10 @@ class MusicPlayerService:
self.player.audio_set_volume(self.volume) self.player.audio_set_volume(self.volume)
logger.info("VLC music player started successfully") logger.info("VLC music player started successfully")
# Automatically load the main playlist
self._load_main_playlist_on_startup()
self._start_sync_thread() self._start_sync_thread()
return True return True
@@ -581,33 +585,29 @@ class MusicPlayerService:
except Exception as e: except Exception as e:
logger.debug(f"Error syncing VLC state: {e}") logger.debug(f"Error syncing VLC state: {e}")
def load_main_playlist(self) -> bool:
"""Load main playlist if available (to be called from within Flask context).""" def _load_main_playlist_on_startup(self):
"""Load the main playlist automatically on startup."""
try: try:
logger.info("Attempting to load main playlist...") if not self.app:
main_playlist = Playlist.query.filter_by(name="Main").first() logger.warning("No Flask app context available, skipping main playlist load")
return
with self.app.app_context():
# Find the main playlist
main_playlist = Playlist.find_main_playlist()
if main_playlist: if main_playlist:
logger.info( success = self.load_playlist(main_playlist.id)
f"Found main playlist with {len(main_playlist.playlist_sounds)} tracks" if success:
) logger.info(f"Automatically loaded main playlist '{main_playlist.name}' with {len(self.playlist_files)} tracks")
result = self.load_playlist(main_playlist.id)
logger.info(f"Load playlist result: {result}")
if result:
logger.info(
f"Successfully loaded main playlist with {len(main_playlist.playlist_sounds)} tracks"
)
return True
else: else:
logger.warning("Failed to load main playlist") logger.warning("Failed to load main playlist on startup")
return False
else: else:
logger.warning( logger.info("No main playlist found to load on startup")
"Main playlist not found, player ready without playlist"
)
return False
except Exception as e: except Exception as e:
logger.error(f"Error loading main playlist: {e}") logger.error(f"Error loading main playlist on startup: {e}")
return False
# Global music player service instance # Global music player service instance