Implemented seeking.

This commit is contained in:
The Wobbler 2024-12-21 20:20:06 +01:00
parent dd44f10832
commit 24d589b172
4 changed files with 51 additions and 10 deletions

View file

@ -9,15 +9,32 @@ PROGRESS_UPDATE_INTERVAL = 1000 // PROGRESS_UPDATE_RATE
class GUICommunication: class GUICommunication:
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
self.window = self.app.gui.window self.window = self.app.gui.window
self.track_control = self.window.main_container.track_control
self.track_progress_slider_dragged = False
self.connect()
self.progress_update_timer = QTimer() self.progress_update_timer = QTimer()
self.progress_update_timer.timeout.connect(self.update_progress) self.progress_update_timer.timeout.connect(self.update_progress)
self.progress_update_timer.start(PROGRESS_UPDATE_INTERVAL) self.progress_update_timer.start(PROGRESS_UPDATE_INTERVAL)
self.on_track_start() self.on_track_start()
def connect(self):
self.track_control.track_progress_slider.sliderPressed.connect(self.on_progress_slider_press)
self.track_control.track_progress_slider.sliderReleased.connect(self.on_progress_slider_release)
def on_progress_slider_press(self):
self.track_progress_slider_dragged = True
def on_progress_slider_release(self):
self.track_progress_slider_dragged = False
self.app.player.seek(self.track_control.track_progress_slider.value())
def on_track_start(self): def on_track_start(self):
self.window.main_container.track_control.track_progress_slider.setRange( self.track_control.track_progress_slider.setRange(
0, 0,
self.app.player.playing_track.duration self.app.player.playing_track.duration
) )
@ -25,14 +42,15 @@ class GUICommunication:
self.update_progress() self.update_progress()
def update_progress(self): def update_progress(self):
remaining = self.app.player.track_progress.timer.remainingTime() if not self.track_progress_slider_dragged:
remaining = self.app.player.track_progress.timer.remainingTime()
if remaining == -1: if remaining == -1:
remaining = self.app.player.track_progress.remaining_time remaining = self.app.player.track_progress.remaining_time
track_duration = self.app.player.playing_track.duration track_duration = self.app.player.playing_track.duration
progress = track_duration - remaining progress = track_duration - remaining
self.window.main_container.track_control.track_progress_slider.setValue(progress) self.track_control.track_progress_slider.setValue(progress)

View file

@ -20,6 +20,7 @@ class Player:
self.playing = False self.playing = False
self.paused = False self.paused = False
self.searched = False
if not file_paths: if not file_paths:
pass pass
@ -31,10 +32,14 @@ class Player:
if self.current_playlist: if self.current_playlist:
self.playing_track = self.current_playlist[0] self.playing_track = self.current_playlist[0]
self.current_playlist_index = 0 self.current_playlist_index = 0
self.playing_segment = self.playing_track.sound
self.playing_segment_duration = self.playing_track.duration
else: else:
self.playing_track = None self.playing_track = None
self.current_playlist_index = 0 self.current_playlist_index = 0
self.playing_segment = None
self.playing_segment_duration = 0
def load_tracks(self, track_paths: list[str]): def load_tracks(self, track_paths: list[str]):
""" """
@ -52,6 +57,8 @@ class Player:
def track_finished(self): def track_finished(self):
self.current_playlist_index += 1 self.current_playlist_index += 1
self.playing_track = self.current_playlist[self.current_playlist_index] self.playing_track = self.current_playlist[self.current_playlist_index]
self.playing_segment = self.playing_track.sound
self.playing_segment_duration = self.playing_track.duration
self.music_channel.play(self.playing_track.sound) self.music_channel.play(self.playing_track.sound)
self.track_progress.start() self.track_progress.start()
@ -86,6 +93,9 @@ class Player:
self.playing_track = self.current_playlist[self.current_playlist_index] self.playing_track = self.current_playlist[self.current_playlist_index]
self.track_progress.stop() self.track_progress.stop()
self.playing_segment = self.playing_track.sound
self.playing_segment_duration = self.playing_track.duration
self.music_channel.play(self.playing_track.sound) self.music_channel.play(self.playing_track.sound)
self.track_progress.start() self.track_progress.start()
self.app.gui_communication.on_track_start() self.app.gui_communication.on_track_start()
@ -105,3 +115,11 @@ class Player:
self.track_progress.stop() self.track_progress.stop()
self.playing = False self.playing = False
def seek(self, position: int):
self.searched = True
self.music_channel.stop()
self.track_progress.stop()
(self.playing_segment, self.playing_segment_duration) = self.playing_track.remaining(position)
self.music_channel.play(self.playing_segment)
self.track_progress.start()

View file

@ -28,4 +28,10 @@ class Track:
return audio, sound, len(audio) return audio, sound, len(audio)
def remaining(self, position: int): def remaining(self, position: int):
return self.audio[-position:] remaining_audio = self.audio[position:]
wav = remaining_audio.export(format="wav")
sound = Sound(wav)
return sound, len(remaining_audio)

View file

@ -17,7 +17,7 @@ class TrackProgress:
self.app.player.track_finished() self.app.player.track_finished()
def start(self): def start(self):
self.timer.start(self.app.player.playing_track.duration) self.timer.start(self.app.player.playing_segment_duration)
def pause(self): def pause(self):
self.remaining_time = self.timer.remainingTime() self.remaining_time = self.timer.remainingTime()
@ -27,6 +27,5 @@ class TrackProgress:
self.timer.start(self.remaining_time) self.timer.start(self.remaining_time)
def stop(self): def stop(self):
print(self.app.player.playing_track.path)
self.timer.stop() self.timer.stop()
self.remaining_time = self.app.player.playing_track.duration self.remaining_time = self.app.player.playing_track.duration