diff --git a/wobuzz/library/library.py b/wobuzz/library/library.py index a460240..1ec3d6f 100644 --- a/wobuzz/library/library.py +++ b/wobuzz/library/library.py @@ -30,15 +30,21 @@ class Library: playlist_view = self.create_playlist_view(playlist) playlist_tabs.playlists[playlist.title] = playlist_view playlist_tabs.addTab(playlist_view, playlist.title) + playlist_view.itemDoubleClicked.connect(self.on_track_doubleclick) def create_playlist_view(self, playlist: Playlist): view = PlaylistView() for track in playlist.tracks: track_item = QTreeWidgetItem(view) + track_item.track = track track_item.setText(1, track.tags.title) track_item.setText(2, track.tags.artist) track_item.setText(3, track.tags.album) return view + def on_track_doubleclick(self, item, column): + index = item.treeWidget().indexOfTopLevelItem(item) + self.app.player.play_track_in_playlist(index) + diff --git a/wobuzz/player/player.py b/wobuzz/player/player.py index 68b7bb5..f433815 100644 --- a/wobuzz/player/player.py +++ b/wobuzz/player/player.py @@ -52,6 +52,15 @@ class Player: self.track_progress.start() self.app.gui_communication.on_track_start() + def play_track_in_playlist(self, track_index): + self.stop() + + self.current_sound, self.current_sound_duration = self.current_playlist.set_track(track_index) + + self.play() + self.track_progress.start() + self.app.gui_communication.on_track_start() + def pause(self): self.music_channel.pause() self.track_progress.pause() diff --git a/wobuzz/player/playlist.py b/wobuzz/player/playlist.py index fa50f83..ac52382 100644 --- a/wobuzz/player/playlist.py +++ b/wobuzz/player/playlist.py @@ -36,11 +36,14 @@ class Playlist: print(lines) i = 0 + num_lines = len(lines) - while i < len(lines): + while i < num_lines: line = lines[i] if line.startswith("#") or line.startswith("http"): # filter out comments, extended m3u and urls + i += 1 + continue self.tracks.append(Track(self.app, line, cache=i==0)) # first track is cached @@ -68,7 +71,6 @@ class Playlist: if not self.current_track.cached: # make sure the track is cached because else the player can't play it self.current_track.cache() - self.current_track.cached = True return self.current_track.sound, self.current_track.duration @@ -81,7 +83,16 @@ class Playlist: if not self.current_track.cached: # make sure the track is cached because else the player can't play it self.current_track.cache() - self.current_track.cached = True + + return self.current_track.sound, self.current_track.duration + + def set_track(self, track_index): + self.current_track_index = track_index + + self.current_track = self.tracks[self.current_track_index] + + if not self.current_track.cached: + self.current_track.cache() return self.current_track.sound, self.current_track.duration diff --git a/wobuzz/player/track.py b/wobuzz/player/track.py index a98e075..a6fd9e2 100644 --- a/wobuzz/player/track.py +++ b/wobuzz/player/track.py @@ -15,15 +15,15 @@ class Track: self.app = app self.path = path self.property_string = property_string - self.cached = cache self.tags = TinyTag.get(self.path) + self.cached = False self.audio = None self.sound = None self.duration = 0 - if self.cached: + if cache: self.cache() def cache(self): @@ -36,6 +36,8 @@ class Track: self.duration = len(self.audio) # track duration in milliseconds + self.cached = True + def remaining(self, position: int): remaining_audio = self.audio[position:] diff --git a/wobuzz/ui/main_window.py b/wobuzz/ui/main_window.py index a6e6cf9..853d5e9 100644 --- a/wobuzz/ui/main_window.py +++ b/wobuzz/ui/main_window.py @@ -10,7 +10,7 @@ class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) - self.setWindowTitle("WoBuzz") + self.setWindowTitle("Wobuzz") self.menu_bar = self.menuBar()