forked from Wobbl/Wobuzz
Added a "Playlist" class.
This commit is contained in:
parent
1190059218
commit
94269fdae4
7 changed files with 119 additions and 53 deletions
80
wobuzz/player/playlist.py
Normal file
80
wobuzz/player/playlist.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import os
|
||||
from .track import Track
|
||||
|
||||
|
||||
class Playlist:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
self.tracks: list[Track] = []
|
||||
self.current_track_index = 0
|
||||
self.current_track: Track | None = None
|
||||
|
||||
def load_from_paths(self, paths):
|
||||
i = 0
|
||||
|
||||
while i < len(paths):
|
||||
path = paths[i]
|
||||
|
||||
if os.path.isfile(path):
|
||||
self.tracks.append(Track(self.app, path, cache=i==0)) # first track is cached
|
||||
|
||||
i += 1
|
||||
|
||||
self.current_track = self.tracks[0] # current track is the first track in the playlist
|
||||
|
||||
def load_from_m3u(self, path):
|
||||
file = open(path, "r")
|
||||
m3u = file.read()
|
||||
file.close()
|
||||
|
||||
lines = m3u.split("\n") # m3u entries
|
||||
|
||||
i = 0
|
||||
|
||||
while i < len(lines):
|
||||
line = lines[i]
|
||||
|
||||
if line.startswith("#") or line.startswith("http"): # filter out comments, extended m3u and urls
|
||||
continue
|
||||
|
||||
self.tracks.append(Track(self.app, line, cache=i==0)) # first track is cached
|
||||
|
||||
i += 1
|
||||
|
||||
def load_from_wbz(self, path):
|
||||
pass
|
||||
|
||||
def has_tracks(self):
|
||||
return len(self.tracks) > 0
|
||||
|
||||
def on_first_track(self):
|
||||
return self.current_track_index == 0
|
||||
|
||||
def on_last_track(self): # if the current track is the last
|
||||
return self.current_track_index == len(self.tracks) - 1
|
||||
|
||||
def next_track(self):
|
||||
self.current_track_index += 1
|
||||
self.current_track = self.tracks[self.current_track_index]
|
||||
|
||||
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 previous_track(self):
|
||||
if self.on_first_track():
|
||||
return self.current_track, self.current_track.duration
|
||||
|
||||
self.current_track_index -= 1
|
||||
self.current_track = self.tracks[self.current_track_index]
|
||||
|
||||
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
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue