3 # Get a test sample with:
4 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
15 def __init__(self, filename, rate):
16 self._filename = filename
19 self._player = gst.element_factory_make("playbin2", "player")
20 self._player.set_property("uri", filename)
21 self._player.connect("about-to-finish", self.on_about_to_finish)
23 bus = self._player.get_bus()
24 bus.add_signal_watch()
25 bus.connect('message::state-changed', self.on_state_changed)
28 self._player.set_state(gst.STATE_PLAYING)
29 loop = gobject.MainLoop()
32 def on_about_to_finish(self, player):
34 player.set_property("uri", self._filename)
36 # XXX do I need to re-set the rate here?
37 # If I uncomment the following line, the player hangs:
38 #self.set_rate(self._date)
40 # And it even hangs if I set the seek_event manually using position=0:
41 #seek_event = gst.event_new_seek(self._rate,
43 # gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
44 # gst.SEEK_TYPE_SET, 0,
45 # gst.SEEK_TYPE_NONE, 0)
46 #self._player.send_event(seek_event)
48 def on_state_changed(self, bus, msg):
49 if msg.src != self._player:
52 print 'on_state_changed'
53 old_state, new_state, pending = msg.parse_state_changed()
54 print "%s -> %s" % (old_state, new_state)
55 if new_state == gst.STATE_PAUSED:
56 self.set_rate(self._rate)
58 def set_rate(self, rate):
61 position, format = self._player.query_position(gst.FORMAT_TIME)
65 # Create the seek event
67 seek_event = gst.event_new_seek(rate,
69 gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
70 gst.SEEK_TYPE_SET, position,
71 gst.SEEK_TYPE_NONE, 0)
73 seek_event = gst.event_new_seek(rate,
75 gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
77 gst.SEEK_TYPE_SET, position)
80 self._player.send_event(seek_event)
81 gst.info("rate set to %s" % rate)
83 gst.warining("change rate failed")
88 sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
94 if not gst.uri_is_valid(args[1]):
95 sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
98 player = Player(args[1], 3.0)
101 if __name__ == '__main__':
102 sys.exit(main(sys.argv))