From edad3875446c26858f6c5296453e9ad95d3cc205 Mon Sep 17 00:00:00 2001 From: Graham Van Heule Date: Sun, 28 May 2023 07:40:53 -0600 Subject: [PATCH] MPRIS: Fix seek actions to correctly move the specified amount of time --- cozy/control/mpris.py | 7 ++++++- cozy/media/player.py | 22 ++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cozy/control/mpris.py b/cozy/control/mpris.py index be147d3e..1e0da3b0 100644 --- a/cozy/control/mpris.py +++ b/cozy/control/mpris.py @@ -228,7 +228,12 @@ def SetPosition(self, track_id, position): self._player.position = position * 10**3 def Seek(self, offset): - self._player.position = self._player.position + offset * 10**3 + # convert milliseconds to seconds + offset_sec=offset / 10**6 + if offset_sec > 0: + self._player.forward(offset_sec) + elif offset_sec < 0: + self._player.rewind(-offset_sec) def Seeked(self, position): self.__bus.emit_signal( diff --git a/cozy/media/player.py b/cozy/media/player.py index a7521633..cf46e862 100644 --- a/cozy/media/player.py +++ b/cozy/media/player.py @@ -156,17 +156,17 @@ def play_pause_chapter(self, book: Book, chapter: Chapter): book.position = chapter.id - def rewind(self): + def rewind(self, duration=None): state = self._gst_player.state if state != GstPlayerState.STOPPED: - self._rewind_in_book() + self._rewind_in_book(duration) if state == GstPlayerState.PLAYING: self._gst_player.play() - def forward(self): + def forward(self, duration=None): state = self._gst_player.state if state != GstPlayerState.STOPPED: - self._forward_in_book() + self._forward_in_book(duration) if state == GstPlayerState.PLAYING: self._gst_player.play() @@ -233,16 +233,19 @@ def _get_playback_path(self, chapter: Chapter): return chapter.file - def _rewind_in_book(self): + def _rewind_in_book(self, rewind_duration=None): if not self._book: log.error("Rewind in book not possible because no book is loaded.") reporter.error("player", "Rewind in book not possible because no book is loaded.") return + if rewind_duration == None: + rewind_duration = self._app_settings.rewind_duration + current_position = self._gst_player.position current_position_relative = max(current_position - self.loaded_chapter.start_position, 0) chapter_number = self._book.chapters.index(self._book.current_chapter) - rewind_seconds = self._app_settings.rewind_duration * self.playback_speed + rewind_seconds = rewind_duration * self.playback_speed if current_position_relative / NS_TO_SEC - rewind_seconds > 0: self._gst_player.position = current_position - NS_TO_SEC * rewind_seconds @@ -254,17 +257,20 @@ def _rewind_in_book(self): else: self._gst_player.position = 0 - def _forward_in_book(self): + def _forward_in_book(self, forward_duration=None): if not self._book: log.error("Forward in book not possible because no book is loaded.") reporter.error("player", "Forward in book not possible because no book is loaded.") return + if forward_duration == None: + forward_duration = self._app_settings.forward_duration + current_position = self._gst_player.position current_position_relative = max(current_position - self.loaded_chapter.start_position, 0) old_chapter = self._book.current_chapter chapter_number = self._book.chapters.index(self._book.current_chapter) - forward_seconds = self._app_settings.forward_duration * self.playback_speed + forward_seconds = forward_duration * self.playback_speed if current_position_relative / NS_TO_SEC + forward_seconds < self._book.current_chapter.length: self._gst_player.position = current_position + (NS_TO_SEC * forward_seconds)