class LocalPlayer QtCore QObject Thread Play local audio track using g

 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
class LocalPlayer(QtCore.QObject, Thread):
"""Play local audio track using gstreamer"""
die = False
paused = True
def __init__(self, config, track, callback = None):
Thread.__init__(self)
QtCore.QObject.__init__(self)
self.track = track
self.config = config
self.player = gst.element_factory_make("playbin2", "player")
fakesink = gst.element_factory_make("fakesink", "fakesink")
self.player.set_property("video-sink", fakesink)
self.bus = self.player.get_bus()
self.bus.add_signal_watch()
self.bus.enable_sync_message_emission()
self.bus.connect('sync-message::element', self.on_sync_message)
self.bus.connect("message", self.on_message)
self.player.set_property("uri", "file://" + track.url)
self.time_format = gst.Format(gst.FORMAT_TIME)
def run(self):
self.play()
def play(self):
self.player.set_state(gst.STATE_PLAYING)
self.reporter()
def reporter(self):
while not self.die:
try:
dur_int = self.player.query_duration(self.time_format, None)[0]
pos_int = self.player.query_position(self.time_format, None)[0]
percent = int(float(pos_int) / dur_int * 100)
self.emit(QtCore.SIGNAL("status"), str(percent))
except:
pass
sleep(0.5)
def on_sync_message(self, *args):
print args
def on_message(self, *args):
print args
def stop(self):
self.player.set_state(gst.STATE_NULL)
def pause(self):
self.player.set_state(gst.STATE_PAUSED)
def seek(self):
pass
def cur_time(self):
time_nano = self.player.query_position(self.time_format, None)[0]
time = time_nano / 1000000000
return int(time)
def total_time(self):
time_nano = self.player.query_duration(self.time_format, None)[0]
time = time_nano / 1000000000
return int(time)
def convert_ns(self, time_int):
time_int = time_int / 1000000000
time_str = ""
if time_int >= 3600:
_hours = time_int / 3600
time_int = time_int - (_hours * 3600)
time_str = str(_hours) + ":"
if time_int >= 600:
_mins = time_int / 60
time_int = time_int - (_mins * 60)
time_str = time_str + str(_mins) + ":"
elif time_int >= 60:
_mins = time_int / 60
time_int = time_int - (_mins * 60)
time_str = time_str + "0" + str(_mins) + ":"
else:
time_str = time_str + "00:"
if time_int > 9:
time_str = time_str + str(time_int)
else:
time_str = time_str + "0" + str(time_int)
return time_str
track = Track('Entwine', 'Losing The Ground', '/home/a2k/Music/Entwine/2001 - Gone/01 - Losing The Ground.flac', 123, local=True)
p = LocalPlayer(None, track)
p.start()
while True:
pass