-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend.py
115 lines (89 loc) · 2.25 KB
/
Backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from SettingsConf import SettingsConf
#http://docs.gstreamer.com/media/sintel_trailer-480p.webm
import threading
import random
import time
#declaring module / bad practice
Gst = None
class Backend(object):
def __init__(self, win):
self.info = SettingsConf()
self.win = win
self.pipeline = None
self.done = False
self.place = 0
self.url_place = 0
self.urls = []
self.last_song = ""
def initServices(self):
self.startThread(self.Services)
def Services(self):
import gi
gi.require_version('Gst','1.0')
from gi.repository import Gst as _Gst
global Gst
Gst = _Gst
Gst.init(None)
from YoutubeExtractor import YoutubeInfoExtractor
self.yi = YoutubeInfoExtractor()
# Load audio for gstreamer to get it ready
self.stream("http://docs.gstreamer.com/media/sintel_trailer-480p.webm")
time.sleep(1)
self.stop()
self.done = True
def startThread(self, func, *args):
t = threading.Thread(target=func,args=args)
t.daemon = True
t.start()
# other actions
def getNextSong(self):
choice = str(self.info.data['Player']['choice_style'])
# Random selection
if choice.lower() == 'random':
song = random.choice(self.urls)
# Make sure it doesn't repick a song
while song == self.last_song:
song = random.choice(self.urls)
self.last_song = song
return song
# Linear selection
else:
# when list ends, restart
if self.url_place > len(self.urls)-1 :
self.url_place = 0
song = self.urls[self.url_place]
self.url_place += 1
return song
def stream(self, url):
try:
self.stop()
del self.pipeline
except:
pass
self.pipeline = Gst.parse_launch("playbin uri=" + url)
self.pipeline.set_state(Gst.State.PLAYING)
def play(self):
try:
self.pipeline.set_state(Gst.State.PLAYING)
except:
pass
def pause(self):
try:
self.pipeline.set_state(Gst.State.PAUSED)
except:
pass
def stop(self):
try:
self.pipeline.set_state(Gst.State.NULL)
except:
pass
def setVolume(self, vol):
try:
self.pipeline.set_property('volume',vol)
except Exception as e:
pass
def getVolume(self):
try:
return self.pipeline.get_property('volume')
except:
return 0