Skip to content

Commit

Permalink
Fix updating track info when player is paused
Browse files Browse the repository at this point in the history
  • Loading branch information
alej0varas committed May 26, 2024
1 parent ccd50ea commit 82c778f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
62 changes: 39 additions & 23 deletions src/bcp/gui.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import time
from datetime import timedelta

import arcade

from . import textures, utils
from . import textures
from .log import get_loger
from .player import Player
from .utils import get_clipboad_content, threaded

_log = get_loger(__name__)

Expand Down Expand Up @@ -175,14 +177,27 @@ def __init__(self, screen_width, screen_height, skip_downloaded=False):
self._current_url = ""
self.url_has_changed = False
self.focus_set = dict()
self.current_track_info = None

def on_click_play(self, *_):
if self.url_has_changed:
self.current_url = self.url_input_text.text
self.url_has_changed = False
if self.current_url:
self.player.setup(self.current_url)
if self.current_url:
self.player.play()
self.update_track_info()

@threaded
def update_track_info(self):
while not self.player.playing:
time.sleep(0.1)
self.current_track_info = {
"title": self.player.get_title(),
"album": self.player.get_album(),
"artist": self.player.get_artist(),
"duration": self.player.get_duration(),
}

def play_update_gui(self):
if not self.player:
Expand All @@ -193,7 +208,7 @@ def play_update_gui(self):
else:
self.play_button.disabled = False
self.pause_button.disabled = True
if self.player.playing:
if hasattr(self.player, "media_player"):
self.next_button.disabled = False
else:
self.next_button.disabled = True
Expand Down Expand Up @@ -225,6 +240,7 @@ def on_click_quit(self, *_):

def handler_music_over(self):
self.player.next()
self.update_track_info()

def on_key_press(self, key, modifiers):
self.keys_held[key] = True
Expand All @@ -236,7 +252,7 @@ def on_key_release(self, key, modifiers):
match key:
case arcade.key.V:
if modifiers & arcade.key.MOD_CTRL:
t = utils.get_clipboad_content()
t = get_clipboad_content()
_log("From clipboard", t)
self.current_url = t
case arcade.key.TAB:
Expand Down Expand Up @@ -287,25 +303,25 @@ def on_draw(self):
self.clear()
self.play_update_gui()

_string = self.player.get_title()
self.text_track_title.text = _string
_string = self.player.get_album()
self.text_track_album.text = _string
_string = self.player.get_artist()
self.text_track_artist.text = _string

_time = self.player.get_time()
milliseconds = int((_time % 1) * 100)
pos_string = "{}.{:02d}".format(
str(timedelta(seconds=int(_time)))[2:], milliseconds
)
_time = self.player.get_duration()
milliseconds = int((_time % 1) * 100)
dur_string = "{}.{:02d}".format(
str(timedelta(seconds=int(_time)))[2:], milliseconds
)
time_string = pos_string + " / " + dur_string
self.text_time.text = time_string
if self.current_track_info:
self.text_track_title.text = self.current_track_info["title"]
self.text_track_album.text = self.current_track_info["album"]
self.text_track_artist.text = self.current_track_info["artist"]

if self.player.playing:
_time = self.player.get_position()
milliseconds = int((_time % 1) * 100)
pos_string = "{}.{:02d}".format(
str(timedelta(seconds=int(_time)))[2:], milliseconds
)
_time = self.current_track_info["duration"]
milliseconds = int((_time % 1) * 100)
dur_string = "{}.{:02d}".format(
str(timedelta(seconds=int(_time)))[2:], milliseconds
)
time_string = pos_string + " / " + dur_string
self.text_time.text = time_string

self.ui.draw()

def _set_focus_on_widget(self, widget):
Expand Down
17 changes: 5 additions & 12 deletions src/bcp/player.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import threading
import time

import arcade
import arcade.gui

from . import bandcamplib
from .log import get_loger
from .utils import threaded

_log = get_loger(__name__)


def threaded(func):
def wrapper(*args, **kwargs):
t = threading.Thread(target=func, args=args, kwargs=kwargs)
t.start()

return wrapper


class Player:
VOLUME_DELTA = 0.1
VOLUME_DELTA_SMALL = 0.01
Expand Down Expand Up @@ -76,6 +68,7 @@ def pause(self):
def next(self):
if not self.media_player:
return
self.playing = False
self.fade_out(0.25)
self.my_music.stop(self.media_player)
self.media_player = None
Expand Down Expand Up @@ -135,13 +128,13 @@ def fade_out(self, duration=1.0):
time.sleep(duration / 100)

def get_volume(self):
if self.playing:
if self.playing and self.media_player:
return self.media_player.volume
return 0.5

def get_time(self):
def get_position(self):
result = 0
if self.playing:
if self.playing and self.media_player:
result = self.media_player.time
return result

Expand Down
9 changes: 9 additions & 0 deletions src/bcp/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
from tkinter import Tk


Expand All @@ -7,3 +8,11 @@ def get_clipboad_content():
t = a.clipboard_get()
a.quit()
return t


def threaded(func):
def wrapper(*args, **kwargs):
t = threading.Thread(target=func, args=args, kwargs=kwargs)
t.start()

return wrapper

0 comments on commit 82c778f

Please sign in to comment.