music-dl/src/controller/player.py

108 lines
2.6 KiB
Python
Raw Normal View History

2018-01-24 17:43:35 -06:00
# -*- coding: utf-8 -*-
import random
2018-02-19 11:45:57 -06:00
import vlc
2018-01-24 17:43:35 -06:00
import logging
from pubsub import pub
from utils import RepeatingTimer
player = None
log = logging.getLogger("player")
def setup():
global player
if player == None:
player = audioPlayer()
class audioPlayer(object):
def __init__(self):
self.is_playing = False
2018-01-26 12:11:14 -06:00
self.vol = 50
2018-01-24 17:43:35 -06:00
self.is_working = False
self.queue = []
self.stopped = True
2018-01-26 11:52:49 -06:00
self.queue_pos = 0
2018-01-26 12:03:07 -06:00
self.shuffle = False
2018-02-19 11:45:57 -06:00
self.instance = vlc.Instance()
self.player = self.instance.media_player_new()
2018-01-24 17:43:35 -06:00
2018-01-25 17:18:51 -06:00
def play(self, item):
2018-02-19 11:45:57 -06:00
self.stopped = True
2018-01-24 17:43:35 -06:00
# Make sure there are no other sounds trying to be played.
if self.is_working == False:
self.is_working = True
2018-01-26 11:52:49 -06:00
if item.download_url == "":
item.get_download_url()
2018-02-19 11:45:57 -06:00
self.stream_new = self.instance.media_new(item.download_url)
self.player.set_media(self.stream_new)
if self.player.play() == -1:
2018-01-25 17:18:51 -06:00
log.debug("Error when playing the file {0}".format(item.title,))
2018-01-26 11:52:49 -06:00
pub.sendMessage("change_status", status=_("Error playing {0}. {1}.").format(item.title, e.description))
self.stopped = True
self.is_working = False
self.next()
2018-01-24 17:43:35 -06:00
return
2018-02-19 11:45:57 -06:00
self.player.audio_set_volume(self.vol)
2018-01-25 17:18:51 -06:00
pub.sendMessage("change_status", status=_("Playing {0}.").format(item.title))
2018-01-24 17:43:35 -06:00
self.stopped = False
self.is_working = False
2018-01-26 11:52:49 -06:00
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])
2018-01-24 17:43:35 -06:00
def stop(self):
2018-02-19 11:45:57 -06:00
self.player.stop()
self.stopped = True
2018-01-24 17:43:35 -06:00
def pause(self):
2018-02-19 11:45:57 -06:00
self.player.pause()
if self.stopped == True:
self.stopped = False
else:
self.stopped = True
2018-01-24 17:43:35 -06:00
@property
def volume(self):
2018-01-25 17:18:51 -06:00
return self.vol
2018-01-24 17:43:35 -06:00
@volume.setter
def volume(self, vol):
if vol <= 100 and vol >= 0:
self.vol = vol
2018-02-19 11:45:57 -06:00
self.player.audio_set_volume(self.vol)
2018-01-24 17:43:35 -06:00
2018-01-26 11:52:49 -06:00
def play_all(self, list_of_items, playing=0, shuffle=False):
if list_of_items != self.queue:
self.queue = list_of_items
self.shuffle = shuffle
self.queue_pos = playing
self.play(self.queue[self.queue_pos])
if not hasattr(self, "worker"):
self.worker = RepeatingTimer(5, self.player_function)
self.worker.start()
2018-01-24 17:43:35 -06:00
def player_function(self):
2018-02-19 11:45:57 -06:00
if self.player.is_playing() == 0 and self.stopped == False:
2018-01-24 17:43:35 -06:00
if len(self.queue) == 0:
return
2018-01-26 11:52:49 -06:00
self.next()