Improved playing experience

This commit is contained in:
Manuel Cortez 2018-01-26 11:52:49 -06:00
parent c9a832b6ee
commit a91e081aa0
2 changed files with 45 additions and 45 deletions

View File

@ -76,29 +76,15 @@ class Controller(object):
self.on_play() self.on_play()
def on_next(self, *args, **kwargs): def on_next(self, *args, **kwargs):
if len(self.results) == 0: return utils.call_threaded(player.player.next)
return
item = self.window.get_item()
if item < len(self.results)-1:
self.window.list.SetSelection(item+1)
else:
self.window.list.SetSelection(0)
return utils.call_threaded(self.play)
def on_previous(self, *args, **kwargs): def on_previous(self, *args, **kwargs):
if len(self.results) == 0: return utils.call_threaded(player.player.previous)
return
item = self.window.get_item()
if item > 0:
self.window.list.SetSelection(item-1)
else:
self.window.list.SetSelection(len(self.results)-1)
return utils.call_threaded(self.play)
def on_play(self, *args, **kwargs): def on_play(self, *args, **kwargs):
items = self.results[self.window.get_item():] items = self.results[::]
return utils.call_threaded(player.player.play_all, items, shuffle=self.window.player_shuffle.IsChecked(), extractor=self.extractor) playing_item = self.window.get_item()
return utils.call_threaded(player.player.play_all, items, playing=playing_item, shuffle=self.window.player_shuffle.IsChecked())
def on_stop(self, *args, **kwargs): def on_stop(self, *args, **kwargs):
player.player.stop() player.player.stop()

View File

@ -26,6 +26,7 @@ class audioPlayer(object):
self.is_working = False self.is_working = False
self.queue = [] self.queue = []
self.stopped = True self.stopped = True
self.queue_pos = 0
def play(self, item): def play(self, item):
if self.stream != None and self.stream.is_playing == True: if self.stream != None and self.stream.is_playing == True:
@ -33,22 +34,20 @@ class audioPlayer(object):
self.stream.stop() self.stream.stop()
except BassError: except BassError:
log.exception("error when stopping the file") log.exception("error when stopping the file")
self.stream = None
self.stopped = True self.stopped = True
if hasattr(self, "worker") and self.worker != None:
self.worker.cancel()
self.worker = None
self.queue = []
# Make sure there are no other sounds trying to be played. # Make sure there are no other sounds trying to be played.
if self.is_working == False: if self.is_working == False:
self.is_working = True self.is_working = True
if item.download_url == "" and hasattr(self, "extractor"): if item.download_url == "":
item.download_url = self.extractor.get_download_url(item.url) item.get_download_url()
try: try:
self.stream = URLStream(url=item.download_url) self.stream = URLStream(url=item.download_url)
except BassError: except BassError as e:
log.debug("Error when playing the file {0}".format(item.title,)) log.debug("Error when playing the file {0}".format(item.title,))
pub.sendMessage("change_status", status=_("Error playing last file")) pub.sendMessage("change_status", status=_("Error playing {0}. {1}.").format(item.title, e.description))
self.stopped = True
self.is_working = False
self.next()
return return
self.stream.volume = self.vol/100.0 self.stream.volume = self.vol/100.0
self.stream.play() self.stream.play()
@ -56,14 +55,32 @@ class audioPlayer(object):
self.stopped = False self.stopped = False
self.is_working = False self.is_working = False
def next(self):
if len(self.queue) > 0:
if self.shuffle:
self.queue_pos = random.randint(0, len(self.queue)-1)
else:
if self.queue_pos < len(self.queue)-1:
self.queue_pos += 1
else:
self.queue_pos = 0
self.play(self.queue[self.queue_pos])
def previous(self):
if len(self.queue) > 0:
if self.shuffle:
self.queue_pos = random.randint(0, len(self.queue)-1)
else:
if self.queue_pos > 0:
self.queue_pos -= 1
else:
self.queue_pos = len(self.queue)-1
self.play(self.queue[self.queue_pos])
def stop(self): def stop(self):
if self.stream != None and self.stream.is_playing == True: if self.stream != None and self.stream.is_playing == True:
self.stream.stop() self.stream.stop()
self.stopped = True self.stopped = True
if hasattr(self, "worker") and self.worker != None:
self.worker.cancel()
self.worker = None
self.queue = []
def pause(self): def pause(self):
if self.stream != None: if self.stream != None:
@ -89,24 +106,21 @@ class audioPlayer(object):
if self.stream != None: if self.stream != None:
self.stream.volume = self.vol/100.0 self.stream.volume = self.vol/100.0
def play_all(self, list_of_urls, shuffle=False, extractor=None): def play_all(self, list_of_items, playing=0, shuffle=False):
self.stop() if list_of_items != self.queue:
self.queue = list_of_urls self.queue = list_of_items
self.extractor = extractor self.shuffle = shuffle
if shuffle: self.queue_pos = playing
random.shuffle(self.queue) self.play(self.queue[self.queue_pos])
self.play(self.queue[0]) if not hasattr(self, "worker"):
self.queue.remove(self.queue[0])
self.worker = RepeatingTimer(5, self.player_function) self.worker = RepeatingTimer(5, self.player_function)
self.worker.start() self.worker.start()
def player_function(self): def player_function(self):
if self.stream != None and self.stream.is_playing == False and self.stopped == False and len(self.stream) == self.stream.position: if self.stream != None and self.stream.is_playing == False and self.stopped == False and len(self.stream) == self.stream.position:
if len(self.queue) == 0: if len(self.queue) == 0:
self.worker.cancel()
return return
self.play(self.queue[0]) self.next()
self.queue.remove(self.queue[0])
def check_is_playing(self): def check_is_playing(self):
if self.stream == None: if self.stream == None: