import gst from time import sleep from threading import Thread from Tr

 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
import gst
from time import sleep
from threading import Thread
from Track import Track
from Length import Length
class PlayerGST(Thread):
track_nped = False
track_subd = False
def __init__(self, track, callback = None):
self.track = track
self.callback = callback
self.player = gst.element_factory_make("playbin", "player")
#b = self.player.get_bus()
#b.add_signal_watch(1)
#b.connect("message", self._player_event)
Thread.__init__(self)
#def start(self):
# self.play()
def run(self):
self.play()
def play(self):
if isinstance(self.track, Track):
url = self.track.url
else:
url = self.track
#self.track.set_playing(True)
self.player.set_property("uri", url)
#self.player.get_bus().add_signal_watch()
#self.player.get_bus().connect("message", self.on_message)
#self.player.get_by_name("file-source").set_property("location", url)
self.player.set_state(gst.STATE_PLAYING)
self.start_callback()
def start_callback(self):
last = None
while self.player.get_state()[1] in (gst.STATE_PLAYING, \
gst.STATE_PAUSED):
dur = self.player.query_duration(gst.FORMAT_TIME, None)[0]
pos = self.player.query_position(gst.FORMAT_TIME, None)[0]
percent = float(float(pos)/float(dur)*100)
#print percent, dur, pos, self.player.get_state()
if (last == percent) and last >99.98:
#self.track.set_playing(False)
self.seek(0)
self.callback('end')
sleep(0.1)
self.player.set_state(gst.STATE_NULL)
return
last = percent
self.callback(float(float(pos)/float(dur)*100))
sleep(.05)
def on_message(self, bus, message):
#return
print message
if message.type == gst.MESSAGE_EOS:
self.player.set_state(gst.STATE_NULL)
self.callback('end')
elif message.type == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.callback('end')
self.player.set_state(gst.STATE_NULL)
def stop(self):
self.track.set_playing(False)
self.player.set_state(gst.STATE_NULL)
def pause(self):
if self.player.get_state()[1] == gst.STATE_PLAYING:
self.player.set_state(gst.STATE_PAUSED)
else:
self.player.set_state(gst.STATE_PLAYING)
def seek(self, pos):
oldpos = self.player.query_position(gst.FORMAT_TIME, None)[0]
print pos
newpos = self.player.query_duration(gst.FORMAT_TIME, None)[0] /100 * pos
#(float(oldpos)/(float(pos)/100))
print oldpos, newpos, self.player.query_duration(gst.FORMAT_TIME, None)[0]
self.player.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, \
newpos)
print newpos / self.player.query_duration(gst.FORMAT_TIME, None)[0] * 100
def cur_time(self):
return self.player.query_position(gst.FORMAT_TIME, \
None)[0]/1000000000
def total_time(self):
return self.player.query_duration(gst.FORMAT_TIME, \
None)[0]/1000000000