Start rewrite of player control module with mpv [Skip CI]

This commit is contained in:
Manuel Cortez 2022-10-20 17:25:05 -05:00
parent f5ecaefe8c
commit 062bbcc842
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790

View File

@ -4,9 +4,8 @@ import random
import logging
import config
import time
from sound_lib import output, stream
import mpv
from pubsub import pub
from utils import call_threaded, RepeatingTimer
player = None
log = logging.getLogger("controller.player")
@ -26,25 +25,26 @@ class audioPlayer(object):
self.stopped = True
self.queue_pos = 0
self.shuffle = False
self.worker = RepeatingTimer(5, self.player_function)
self.worker.start()
self.output = output.Output()
self.set_output_device(config.app["main"]["output_device"])
self.player = None
self.player = mpv.MPV()
# Fires at the end of every file and attempts to play the next one.
@self.player.event_callback('end-file')
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):
""" Retrieve enabled output devices so we can switch or use those later. """
devices = output.Output.get_device_names()
return devices
return None
def set_output_device(self, device_name):
""" Set Output device to be used in LibVLC"""
log.debug("Setting output audio device to {device}...".format(device=device_name,))
try:
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"
# config.app["main"]["output_device"] = "Default"
def play(self, item):
self.stopped = True
@ -53,17 +53,8 @@ class audioPlayer(object):
if item.download_url == "":
item.get_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.stop()
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
self.player.play(item.download_url)
self.player.volume = self.vol
pub.sendMessage("change_status", status=_("Playing {0}.").format(item.title))
self.stopped = False
self.is_working = False
@ -95,7 +86,7 @@ class audioPlayer(object):
self.stopped = True
def pause(self):
self.player.pause()
self.player.pause = True
if self.stopped == True:
self.stopped = False
else:
@ -111,7 +102,7 @@ class audioPlayer(object):
config.app["main"]["volume"] = vol
self.vol = vol
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):
if list_of_items != self.queue:
@ -119,17 +110,3 @@ class audioPlayer(object):
self.shuffle = shuffle
self.queue_pos = playing
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."))