This repository has been archived on 2022-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
zeropod/plugin/music/player.py

67 lines
2.2 KiB
Python
Raw Normal View History

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 12:38:16 -06:00
self._init_now_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 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:38:16 -06:00
def _init_now_playing(self, track_name: str):
2022-12-28 12:26:06 -06:00
self._graphics.clear()
2022-12-28 12:38:16 -06:00
self._graphics.text(track_name, 0, 0, 1)
self._graphics.text("Pos:", 0, 1, 1)
self._draw_progress(0, 0)
2022-12-28 12:27:37 -06:00
self._graphics.show()
2022-12-28 12:38:16 -06:00
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
return "%d:%02d:%02d" % (hours, minutes, seconds)