2022-12-28 12:26:06 -06:00
|
|
|
from os import path
|
2022-12-28 11:26:30 -06:00
|
|
|
from menu import Menu, MenuType
|
2022-12-28 10:14:33 -06:00
|
|
|
from cinput import ControlInput
|
|
|
|
from graphics import Graphics
|
2022-12-28 11:53:20 -06:00
|
|
|
from icecream import ic
|
2022-12-28 11:50:37 -06:00
|
|
|
from mpv import MPV
|
2022-12-28 10:14:33 -06:00
|
|
|
|
|
|
|
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:
|
2022-12-28 11:26:30 -06:00
|
|
|
item = self._menu.get_selection()
|
2022-12-28 11:50:37 -06:00
|
|
|
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)
|
2022-12-28 11:55:03 -06:00
|
|
|
elif item.data["mode"] == "single":
|
2022-12-28 12:26:06 -06:00
|
|
|
self._play_track(item.data["dir"], item.data["track"])
|
2022-12-28 11:50:37 -06:00
|
|
|
elif item.data["mode"] == "resume":
|
|
|
|
self._play_resume(item.data["dir"])
|
|
|
|
elif item.menu_type == MenuType.BACK:
|
2022-12-28 11:26:30 -06:00
|
|
|
return
|
2022-12-28 11:50:37 -06:00
|
|
|
|
|
|
|
def _play_dir(self, directory: str, random: bool):
|
2022-12-28 11:55:03 -06:00
|
|
|
ic("Playing", directory, random)
|
2022-12-28 11:50:37 -06:00
|
|
|
return
|
|
|
|
|
2022-12-28 12:26:06 -06:00
|
|
|
def _play_track(self, directory: str, track: str):
|
2022-12-28 11:55:03 -06:00
|
|
|
ic("Playing", track)
|
2022-12-28 11:50:37 -06:00
|
|
|
player = MPV()
|
2022-12-28 12:26:06 -06:00
|
|
|
player.play(path.join(directory, track))
|
2022-12-28 12:53:20 -06:00
|
|
|
self._run_now_playing(player, track)
|
2022-12-28 11:50:37 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
def _play_resume(self, directory: str):
|
2022-12-28 11:55:03 -06:00
|
|
|
ic("Resuming", directory)
|
2022-12-28 11:50:37 -06:00
|
|
|
return
|
2022-12-28 12:26:06 -06:00
|
|
|
|
2022-12-28 12:53:20 -06:00
|
|
|
def _run_now_playing(self, player: MPV, init_name: str):
|
2022-12-28 12:26:06 -06:00
|
|
|
self._graphics.clear()
|
2022-12-28 12:53:20 -06:00
|
|
|
self._graphics.text(init_name, 0, 0, 1)
|
2022-12-28 12:38:16 -06:00
|
|
|
self._graphics.text("Pos:", 0, 1, 1)
|
2022-12-28 12:48:42 -06:00
|
|
|
self._draw_progress(0, int(player.properties["duration"] or 0))
|
2022-12-28 12:27:37 -06:00
|
|
|
self._graphics.show()
|
2022-12-28 12:48:42 -06:00
|
|
|
player.wait_for_playback()
|
2022-12-28 12:38:16 -06:00
|
|
|
|
|
|
|
def _draw_progress(self, pos: int, length: int):
|
|
|
|
pos_str = self._convert_seconds(pos)
|
2022-12-28 12:48:42 -06:00
|
|
|
if length <= 0:
|
2022-12-28 12:38:16 -06:00
|
|
|
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
|
|
|
|
return "%d:%02d:%02d" % (hours, minutes, seconds)
|