2016-02-15 02:15:38 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-01-01 20:04:30 -06:00
|
|
|
import sys
|
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
|
2019-01-15 11:43:32 -06:00
|
|
|
import config
|
|
|
|
from sound_lib.config import BassConfig
|
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 = []
|
2019-01-22 17:41:39 -06:00
|
|
|
self.playing_track = 0
|
2016-03-23 08:17:45 -06:00
|
|
|
self.stopped = True
|
2019-01-15 11:43:32 -06:00
|
|
|
# Modify some default settings present in Bass so it will increase timeout connection, thus causing less "connection timed out" errors when playing.
|
|
|
|
bassconfig = BassConfig()
|
|
|
|
# Set timeout connection to 30 seconds.
|
|
|
|
bassconfig["net_timeout"] = 30000
|
|
|
|
# Adds proxy settings
|
|
|
|
if config.app["app-settings"]["use_proxy"] == True:
|
|
|
|
bassconfig["net_proxy"] = b"socializer:socializer@socializer.su:3128"
|
2016-02-15 02:15:38 -06:00
|
|
|
|
2019-01-22 17:41:39 -06:00
|
|
|
def play(self, url, set_info=True, fresh=False):
|
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
|
2019-01-22 17:41:39 -06:00
|
|
|
if fresh == True and hasattr(self, "worker") and self.worker != None:
|
2016-03-25 11:06:02 -06:00
|
|
|
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
|
2019-01-01 20:04:30 -06:00
|
|
|
# Let's encode the URL as bytes if on Python 3
|
|
|
|
if sys.version[0] == "3":
|
|
|
|
url_ = bytes(url["url"], "utf-8")
|
|
|
|
else:
|
|
|
|
url_ = url["url"]
|
2016-09-19 17:13:40 -05:00
|
|
|
try:
|
2019-01-01 20:04:30 -06:00
|
|
|
self.stream = URLStream(url=url_)
|
2019-01-15 11:43:32 -06:00
|
|
|
except IndexError:
|
|
|
|
log.error("Unable to play URL")
|
|
|
|
log.error(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:
|
2019-01-01 19:42:53 -06:00
|
|
|
msg = _("Playing {0} by {1}").format(url["title"], url["artist"])
|
2016-09-25 14:24:40 -05:00
|
|
|
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):
|
2019-01-22 16:40:00 -06:00
|
|
|
return self.vol
|
2016-02-15 02:15:38 -06:00
|
|
|
|
|
|
|
@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):
|
2019-01-22 17:41:39 -06:00
|
|
|
self.playing_track = 0
|
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)
|
2019-01-22 17:41:39 -06:00
|
|
|
self.play(self.queue[self.playing_track])
|
2016-03-23 08:17:45 -06:00
|
|
|
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:
|
2019-01-22 17:41:39 -06:00
|
|
|
if len(self.queue) == 0 or self.playing_track >= len(self.queue):
|
2016-03-23 08:17:45 -06:00
|
|
|
self.worker.cancel()
|
|
|
|
return
|
2019-01-22 17:41:39 -06:00
|
|
|
if self.playing_track < len(self.queue):
|
|
|
|
self.playing_track += 1
|
|
|
|
self.play(self.queue[self.playing_track])
|
|
|
|
|
|
|
|
def play_next(self):
|
|
|
|
if len(self.queue) == 0:
|
|
|
|
return
|
|
|
|
if self.playing_track < len(self.queue):
|
|
|
|
self.playing_track += 1
|
|
|
|
else:
|
|
|
|
self.playing_track = 0
|
|
|
|
self.play(self.queue[self.playing_track])
|
|
|
|
|
|
|
|
def play_previous(self):
|
|
|
|
if len(self.queue) == 0:
|
|
|
|
return
|
|
|
|
if self.playing_track <= 0:
|
|
|
|
self.playing_track = len(self.queue)-1
|
|
|
|
else:
|
|
|
|
self.playing_track -= 1
|
|
|
|
self.play(self.queue[self.playing_track])
|
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
|
|
|
|
|