2016-02-15 02:15:38 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-03-13 02:16:34 -06:00
|
|
|
import random
|
2016-03-26 12:02:19 -06:00
|
|
|
import output
|
2016-02-15 02:15:38 -06:00
|
|
|
import sound_lib
|
2016-09-25 14:24:40 -05:00
|
|
|
import logging
|
|
|
|
|
2016-02-15 02:15:38 -06:00
|
|
|
from sound_lib.stream import URLStream
|
2016-09-19 17:13:40 -05:00
|
|
|
from sound_lib.main import BassError
|
2016-03-23 08:17:45 -06:00
|
|
|
from mysc.repeating_timer import RepeatingTimer
|
2016-03-26 09:08:41 -06:00
|
|
|
from pubsub import pub
|
2016-02-15 02:15:38 -06:00
|
|
|
|
|
|
|
player = None
|
2016-09-25 14:24:40 -05:00
|
|
|
log = logging.getLogger("player")
|
2016-02-15 02:15:38 -06:00
|
|
|
|
|
|
|
def setup():
|
|
|
|
global player
|
|
|
|
if player == None:
|
|
|
|
player = audioPlayer()
|
|
|
|
|
|
|
|
class audioPlayer(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.is_playing = False
|
|
|
|
self.stream = None
|
|
|
|
self.vol = 100
|
2016-02-19 17:30:11 -06:00
|
|
|
self.is_working = False
|
2016-03-23 08:17:45 -06:00
|
|
|
self.queue = []
|
|
|
|
self.stopped = True
|
2016-02-15 02:15:38 -06:00
|
|
|
|
2016-09-25 14:24:40 -05:00
|
|
|
def play(self, url, set_info=True):
|
2016-02-15 02:15:38 -06:00
|
|
|
if self.stream != None and self.stream.is_playing == True:
|
2016-09-19 17:13:40 -05:00
|
|
|
try:
|
|
|
|
self.stream.stop()
|
|
|
|
except BassError:
|
|
|
|
log.exception("error when stopping the file")
|
|
|
|
self.stream = None
|
2016-03-23 08:17:45 -06:00
|
|
|
self.stopped = True
|
2016-03-25 11:06:02 -06:00
|
|
|
if hasattr(self, "worker") and self.worker != None:
|
|
|
|
self.worker.cancel()
|
|
|
|
self.worker = None
|
|
|
|
self.queue = []
|
2016-02-19 17:30:11 -06:00
|
|
|
# Make sure that there are no other sounds trying to be played.
|
|
|
|
if self.is_working == False:
|
|
|
|
self.is_working = True
|
2016-09-19 17:13:40 -05:00
|
|
|
try:
|
|
|
|
self.stream = URLStream(url=url["url"])
|
|
|
|
except BassError:
|
2016-09-25 14:24:40 -05:00
|
|
|
log.debug("Error when playing the file %r") % (url,)
|
2016-09-19 17:13:40 -05:00
|
|
|
return
|
2016-06-29 10:56:41 -05:00
|
|
|
# Translators: {0} will be replaced with a song's title and {1} with the artist.
|
2016-09-25 14:24:40 -05:00
|
|
|
if set_info:
|
|
|
|
msg = _(u"Playing {0} by {1}").format(url["title"], url["artist"])
|
|
|
|
pub.sendMessage("update-status-bar", status=msg)
|
2016-02-19 17:30:11 -06:00
|
|
|
self.stream.volume = self.vol/100.0
|
|
|
|
self.stream.play()
|
2016-03-23 08:17:45 -06:00
|
|
|
self.stopped = False
|
2016-02-19 17:30:11 -06:00
|
|
|
self.is_working = False
|
2016-02-15 02:15:38 -06:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.stream != None and self.stream.is_playing == True:
|
|
|
|
self.stream.stop()
|
2016-03-23 08:17:45 -06:00
|
|
|
self.stopped = True
|
|
|
|
if hasattr(self, "worker") and self.worker != None:
|
|
|
|
self.worker.cancel()
|
|
|
|
self.worker = None
|
|
|
|
self.queue = []
|
2016-02-15 02:15:38 -06:00
|
|
|
|
|
|
|
def pause(self):
|
2016-02-19 17:36:19 -06:00
|
|
|
if self.stream != None:
|
|
|
|
if self.stream.is_playing == True:
|
|
|
|
self.stream.pause()
|
2016-03-23 08:17:45 -06:00
|
|
|
self.stopped = True
|
2016-02-19 17:36:19 -06:00
|
|
|
else:
|
2016-09-19 17:13:40 -05:00
|
|
|
try:
|
|
|
|
self.stream.play()
|
|
|
|
self.stopped = False
|
|
|
|
except BassError:
|
|
|
|
pass
|
2016-02-15 02:15:38 -06:00
|
|
|
|
|
|
|
@property
|
|
|
|
def volume(self):
|
|
|
|
if self.stream != None:
|
|
|
|
return self.vol
|
|
|
|
|
|
|
|
@volume.setter
|
|
|
|
def volume(self, vol):
|
2016-02-15 05:42:50 -06:00
|
|
|
if vol <= 100 and vol >= 0:
|
|
|
|
self.vol = vol
|
2016-02-15 02:15:38 -06:00
|
|
|
if self.stream != None:
|
2016-02-15 05:42:50 -06:00
|
|
|
self.stream.volume = self.vol/100.0
|
2016-02-15 02:15:38 -06:00
|
|
|
|
2017-03-13 02:16:34 -06:00
|
|
|
def play_all(self, list_of_urls, shuffle=False):
|
2016-05-14 20:33:16 -05:00
|
|
|
self.stop()
|
2018-12-16 00:53:02 -06:00
|
|
|
# Skip all country restricted tracks as they are not playable here.
|
|
|
|
self.queue = [i for i in list_of_urls if i["url"] != ""]
|
2017-03-13 02:16:34 -06:00
|
|
|
if shuffle:
|
|
|
|
random.shuffle(self.queue)
|
2016-03-23 08:17:45 -06:00
|
|
|
self.play(self.queue[0])
|
|
|
|
self.queue.remove(self.queue[0])
|
|
|
|
self.worker = RepeatingTimer(5, self.player_function)
|
|
|
|
self.worker.start()
|
|
|
|
|
|
|
|
def player_function(self):
|
2016-07-10 22:59:59 -05:00
|
|
|
if self.stream != None and self.stream.is_playing == False and self.stopped == False and len(self.stream) == self.stream.position:
|
2016-03-23 08:17:45 -06:00
|
|
|
if len(self.queue) == 0:
|
|
|
|
self.worker.cancel()
|
|
|
|
return
|
|
|
|
self.play(self.queue[0])
|
|
|
|
self.queue.remove(self.queue[0])
|
2016-07-18 17:34:37 -05:00
|
|
|
|
|
|
|
def check_is_playing(self):
|
|
|
|
if self.stream == None:
|
|
|
|
return False
|
|
|
|
if self.stream != None and self.stream.is_playing == False:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|