6 from sound_play.msg
import SoundRequest
10 gi.require_version(
'Gst',
'1.0')
11 from gi.repository
import Gst
as Gst
14 **************************************************************
15 Error opening pygst. Is gstreamer installed?
16 **************************************************************
29 self.
lock = threading.RLock()
31 self.
sound = Gst.ElementFactory.make(
"playbin",
None)
32 if self.
sound is None:
33 raise Exception(
"Could not create sound player")
36 self.
sink = Gst.ElementFactory.make(
"alsasink",
"sink")
37 self.
sink.set_property(
"device", device)
38 self.
sound.set_property(
"audio-sink", self.
sink)
42 elif os.path.isfile(file):
43 uri =
"file://" + os.path.abspath(file)
45 rospy.logerr(
'Error: URI is invalid: %s' % file)
49 self.
sound.set_property(
'uri', uri)
50 self.
sound.set_property(
"volume", volume)
55 self.
bus.add_signal_watch()
59 if message.type == Gst.MessageType.EOS:
61 self.
sound.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0)
70 if self.
bus is not None:
71 self.
bus.poll(Gst.MessageType.ERROR, 10)
81 self.
sound.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0)
82 self.
sound.set_state(Gst.State.PLAYING)
90 if self.
bus is not None:
91 self.
sound.set_state(Gst.State.NULL)
93 self.
bus.remove_signal_watch()
98 except Exception
as e:
99 rospy.logerr(
'Exception in dispose: %s' %
str(e))
107 self.
sound.set_state(Gst.State.NULL)
115 rospy.logdebug(
"Playing %s" % self.
uri)
120 self.
sound.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0)
121 self.
sound.set_state(Gst.State.PLAYING)
127 if cmd == SoundRequest.PLAY_STOP:
129 elif cmd == SoundRequest.PLAY_ONCE:
131 elif cmd == SoundRequest.PLAY_START:
139 position = self.
sound.query_position(Gst.Format.TIME)[1]
140 duration = self.
sound.query_duration(Gst.Format.TIME)[1]
147 if position != duration: