Skip to content

Commit

Permalink
fix: try to set status only with fade_lock acquired
Browse files Browse the repository at this point in the history
  • Loading branch information
mokurin000 committed Jan 21, 2024
1 parent 12f0b00 commit 42c25c6
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions feeluown/player/mpvplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def set_play_range(self, start=None, end=None):
self.seeked.emit(start)
_mpv_set_option_string(self._mpv.handle, b'end', bytes(end_str, 'utf-8'))

def fade(self, fade_in: bool, callback=None):
def fade(self, fade_in: bool):
# k: factor between 0 and 1, to represent tick/fade_time
def fade_curve(k: float, fade_in: bool) -> float:
if fade_in:
Expand All @@ -186,35 +186,42 @@ def set_volume(max_volume: int, fade_in: bool):

with self.fade_lock:
max_volume = self.volume
set_volume(max_volume, fade_in=fade_in)

# skip fade-in on playing and fade-out on pause
if fade_in != self._mpv.pause:
return

if callback is not None:
callback()
if fade_in:
self._resume()
set_volume(max_volume, fade_in=True)
else:
set_volume(max_volume, fade_in=True)
self._pause()

self.volume = max_volume

def resume(self):
if self.do_fade:
fade_thread = Thread(
target=self.fade,
kwargs={"fade_in": True}
)
fade_thread.start()

def _resume(self):
self._mpv.pause = False
self.state = State.playing

def _pause(self):
self._mpv.pause = True
self.state = State.paused

def resume(self):
if self.do_fade:
fade_thread = Thread(
target=self.fade,
kwargs={"fade_in": True})
fade_thread.start()
else:
self._resume()

def pause(self):
if self.do_fade:
fade_thread = Thread(
target=self.fade,
kwargs={"fade_in": False,
"callback": self._pause}
)
kwargs={"fade_in": False})
fade_thread.start()
else:
self._pause()
Expand Down

0 comments on commit 42c25c6

Please sign in to comment.