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

81 lines
2.6 KiB
Python
Raw Permalink Normal View History

2022-12-28 12:26:06 -06:00
from os import path
2022-12-28 13:02:58 -06:00
from time import sleep
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:59:10 -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 12:59:10 -06:00
self._run_now_playing(player)
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:59:10 -06:00
def _init_now_playing(self, 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:59:10 -06:00
self._draw_progress(0, 0)
2022-12-28 12:27:37 -06:00
self._graphics.show()
2022-12-28 12:59:10 -06:00
def _run_now_playing(self, player: MPV):
2022-12-28 13:02:58 -06:00
player.wait_until_playing()
ic(player.properties["filename"])
while not player.core_idle:
sleep(0.5)
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)
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
2022-12-28 12:59:10 -06:00
if hours > 0:
return "%d:%02d:%02d" % (hours, minutes, seconds)
elif minutes > 0:
return "%d:%02d" % (minutes, seconds)
else:
return "%02d" % (seconds)