Start rewrite of player control module with mpv [Skip CI]
This commit is contained in:
		@@ -4,9 +4,8 @@ import random
 | 
				
			|||||||
import logging
 | 
					import logging
 | 
				
			||||||
import config
 | 
					import config
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
from sound_lib import output, stream
 | 
					import mpv
 | 
				
			||||||
from pubsub import pub
 | 
					from pubsub import pub
 | 
				
			||||||
from utils import call_threaded, RepeatingTimer
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
player = None
 | 
					player = None
 | 
				
			||||||
log = logging.getLogger("controller.player")
 | 
					log = logging.getLogger("controller.player")
 | 
				
			||||||
@@ -26,25 +25,26 @@ class audioPlayer(object):
 | 
				
			|||||||
        self.stopped = True
 | 
					        self.stopped = True
 | 
				
			||||||
        self.queue_pos = 0
 | 
					        self.queue_pos = 0
 | 
				
			||||||
        self.shuffle = False
 | 
					        self.shuffle = False
 | 
				
			||||||
        self.worker = RepeatingTimer(5, self.player_function)
 | 
					        self.player = mpv.MPV()
 | 
				
			||||||
        self.worker.start()
 | 
					
 | 
				
			||||||
        self.output = output.Output()
 | 
					        # Fires at the end of every file and attempts to play the next one.
 | 
				
			||||||
        self.set_output_device(config.app["main"]["output_device"])
 | 
					        @self.player.event_callback('end-file')
 | 
				
			||||||
        self.player = None
 | 
					        def handle_end_idle(event):
 | 
				
			||||||
 | 
					            if event.as_dict()["reason"] == b"aborted" or event.as_dict()["reason"] == b"stop":
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					            log.debug("Reached end of file stream.")
 | 
				
			||||||
 | 
					            if len(self.queue) > 1:
 | 
				
			||||||
 | 
					                log.debug("Requesting next item...")
 | 
				
			||||||
 | 
					                self.next()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_output_devices(self):
 | 
					    def get_output_devices(self):
 | 
				
			||||||
        """ Retrieve enabled output devices so we can switch or use those later. """
 | 
					        """ Retrieve enabled output devices so we can switch or use those later. """
 | 
				
			||||||
        devices = output.Output.get_device_names()
 | 
					        return None
 | 
				
			||||||
        return devices
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def set_output_device(self, device_name):
 | 
					    def set_output_device(self, device_name):
 | 
				
			||||||
        """ Set Output device to be used in LibVLC"""
 | 
					        """ Set Output device to be used in LibVLC"""
 | 
				
			||||||
        log.debug("Setting output audio device to {device}...".format(device=device_name,))
 | 
					        log.debug("Setting output audio device to {device}...".format(device=device_name,))
 | 
				
			||||||
        try:
 | 
					#            config.app["main"]["output_device"] = "Default"
 | 
				
			||||||
            self.output.set_device(self.output.find_device_by_name(device_name))
 | 
					 | 
				
			||||||
        except:
 | 
					 | 
				
			||||||
            log.error("Error in input or output devices, using defaults...")
 | 
					 | 
				
			||||||
            config.app["main"]["output_device"] = "Default"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def play(self, item):
 | 
					    def play(self, item):
 | 
				
			||||||
        self.stopped = True
 | 
					        self.stopped = True
 | 
				
			||||||
@@ -53,17 +53,8 @@ class audioPlayer(object):
 | 
				
			|||||||
            if item.download_url == "":
 | 
					            if item.download_url == "":
 | 
				
			||||||
                item.get_download_url()
 | 
					                item.get_download_url()
 | 
				
			||||||
            log.debug("playing {0}...".format(item.download_url,))
 | 
					            log.debug("playing {0}...".format(item.download_url,))
 | 
				
			||||||
            if hasattr(self, "player") and self.player != None and self.player.is_playing:
 | 
					            self.player.play(item.download_url)
 | 
				
			||||||
                self.player.stop()
 | 
					            self.player.volume = self.vol
 | 
				
			||||||
            self.player = stream.URLStream(item.download_url)
 | 
					 | 
				
			||||||
            if self.player.play() == -1:
 | 
					 | 
				
			||||||
                log.debug("Error when playing the file {0}".format(item.title,))
 | 
					 | 
				
			||||||
                pub.sendMessage("change_status", status=_("Error playing {0}. {1}.").format(item.title, e.description))
 | 
					 | 
				
			||||||
                self.stopped = True
 | 
					 | 
				
			||||||
                self.is_working = False
 | 
					 | 
				
			||||||
                self.next()
 | 
					 | 
				
			||||||
                return
 | 
					 | 
				
			||||||
            self.player.volume = self.vol/100
 | 
					 | 
				
			||||||
            pub.sendMessage("change_status", status=_("Playing {0}.").format(item.title))
 | 
					            pub.sendMessage("change_status", status=_("Playing {0}.").format(item.title))
 | 
				
			||||||
            self.stopped = False
 | 
					            self.stopped = False
 | 
				
			||||||
            self.is_working = False
 | 
					            self.is_working = False
 | 
				
			||||||
@@ -95,7 +86,7 @@ class audioPlayer(object):
 | 
				
			|||||||
        self.stopped = True
 | 
					        self.stopped = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def pause(self):
 | 
					    def pause(self):
 | 
				
			||||||
        self.player.pause()
 | 
					        self.player.pause = True
 | 
				
			||||||
        if self.stopped == True:
 | 
					        if self.stopped == True:
 | 
				
			||||||
            self.stopped = False
 | 
					            self.stopped = False
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
@@ -111,7 +102,7 @@ class audioPlayer(object):
 | 
				
			|||||||
            config.app["main"]["volume"] = vol
 | 
					            config.app["main"]["volume"] = vol
 | 
				
			||||||
            self.vol = vol
 | 
					            self.vol = vol
 | 
				
			||||||
        if self.player != None:
 | 
					        if self.player != None:
 | 
				
			||||||
            self.player.volume = self.vol/100
 | 
					            self.player.volume = self.vol
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def play_all(self, list_of_items, playing=0, shuffle=False):
 | 
					    def play_all(self, list_of_items, playing=0, shuffle=False):
 | 
				
			||||||
        if list_of_items != self.queue:
 | 
					        if list_of_items != self.queue:
 | 
				
			||||||
@@ -119,17 +110,3 @@ class audioPlayer(object):
 | 
				
			|||||||
        self.shuffle = shuffle
 | 
					        self.shuffle = shuffle
 | 
				
			||||||
        self.queue_pos = playing
 | 
					        self.queue_pos = playing
 | 
				
			||||||
        self.play(self.queue[self.queue_pos])
 | 
					        self.play(self.queue[self.queue_pos])
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def player_function(self):
 | 
					 | 
				
			||||||
        """ Check if the stream has reached the end of the file  so it will play the next song. """
 | 
					 | 
				
			||||||
        if self.player != None and self.player.is_playing == False and self.stopped == False and len(self.player)-self.player.position < 50000:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if self.queue_pos >= len(self.queue):
 | 
					 | 
				
			||||||
                self.stopped = True
 | 
					 | 
				
			||||||
                return
 | 
					 | 
				
			||||||
            elif self.queue_pos < len(self.queue):
 | 
					 | 
				
			||||||
                self.queue_pos += 1
 | 
					 | 
				
			||||||
            self.play(self.queue[self.queue_pos])
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def playback_error(self, event):
 | 
					 | 
				
			||||||
        pub.sendMessage("notify", title=_("Error"), message=_("There was an error while trying to access the file you have requested."))
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user