from os import path from menu import Menu, MenuType from cinput import ControlInput from graphics import Graphics from icecream import ic from mpv import MPV class MusicPlayer: def __init__(self, cinput: ControlInput, graphics: Graphics, menu: Menu): self._cinput = cinput self._graphics = graphics self._menu = menu def run(self): while True: item = self._menu.get_selection() if item.menu_type == MenuType.CMD: if item.data["mode"] == "album": self._play_dir(item.data["dir"], False) elif item.data["mode"] == "random": self._play_dir(item.data["dir"], True) elif item.data["mode"] == "single": self._play_track(item.data["dir"], item.data["track"]) elif item.data["mode"] == "resume": self._play_resume(item.data["dir"]) elif item.menu_type == MenuType.BACK: return def _play_dir(self, directory: str, random: bool): ic("Playing", directory, random) return def _play_track(self, directory: str, track: str): ic("Playing", track) self._init_now_playing(track) player = MPV() player.play(path.join(directory, track)) self._run_now_playing(player) return def _play_resume(self, directory: str): ic("Resuming", directory) return def _init_now_playing(self, init_name: str): self._graphics.clear() self._graphics.text(init_name, 0, 0, 1) self._graphics.text("Pos:", 0, 1, 1) self._draw_progress(0, 0) self._graphics.show() def _run_now_playing(self, player: MPV): player.wait_for_playback() def _draw_progress(self, pos: int, length: int): pos_str = self._convert_seconds(pos) if length <= 0: self._graphics.text(pos_str, 5, 1, 1) else: len_str = self._convert_seconds(length) progress_str = "%s / %s" % (pos_str, len_str) self._graphics.text(progress_str, 5, 1, 1) def _convert_seconds(self, seconds: int): seconds = seconds % (24 * 3600) hours = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60 if hours > 0: return "%d:%02d:%02d" % (hours, minutes, seconds) elif minutes > 0: return "%d:%02d" % (minutes, seconds) else: return "%02d" % (seconds)