gst-vocoder.sh: use vocoder_1337.so from swh-plugins
[experiments/gstreamer.git] / python / gst-player-example.py
1 #!/usr/bin/env python3
2 #
3 # Player - a very simple media player based on GstPlayer
4 #
5 # Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import sys
21 import gi
22
23 gi.require_version('Gst', '1.0')
24 from gi.repository import Gst
25 Gst.init(None)
26
27 gi.require_version('GstPlayer', '1.0')
28 from gi.repository import GstPlayer
29
30 from gi.repository import GObject
31 GObject.threads_init()
32
33
34 class Player(object):
35     def __init__(self):
36         self.player = GstPlayer.Player.new(signal_dispatcher=GstPlayer.PlayerGMainContextSignalDispatcher())
37         self.player.connect("error", self.error_cb)
38         self.player.connect("end-of-stream", self.end_of_stream_cb)
39         self.player.connect("state-changed", self.state_changed_cb)
40
41         self.mainloop = GObject.MainLoop()
42
43     def end_of_stream_cb(self, player):
44         self.mainloop.quit()
45
46     def error_cb(self, player, error):
47         print(error)
48         self.mainloop.quit()
49
50     def state_changed_cb(self, player, state):
51         print(state)
52
53     def play(self, filename):
54         self.player.set_uri(Gst.filename_to_uri(filename))
55         self.player.play()
56         self.mainloop.run()
57
58     def stop(self):
59         self.player.stop()
60         self.mainloop.quit()
61
62
63 def main():
64     if len(sys.argv) > 1:
65         player = Player()
66         try:
67             player.play(sys.argv[1])
68         except KeyboardInterrupt:
69             player.stop()
70
71
72 if __name__ == "__main__":
73     main()