문제

I am using gstreamer with python to play some music and need to trigger an event when playback has finished. I have tried this using the method provided in the tutorial but it simply does nothing when the track finishes. Here is the code I'm using:

class Slave(object):
  """Provides methods for playback on slave devices"""
  def __init__(self):
    self.playlist = []
    self.playlist_index = 0
    self.playlist_length = 0
    self.player = gst.element_factory_make('playbin2', 'player')
    fakesink = gst.element_factory_make('fakesink', 'fakesink')
    self.player.set_property('video-sink', fakesink)
    bus = self.player.get_bus()
    bus.add_signal_watch()
    bus.connect('message', self.on_message)

  def play_list(self, playlist):
    """Starts playing the playlist provided"""
    self.playlist = playlist
    self.playlist_index = 0
    self.playlist_length = len(self.playlist)
    self.player.set_property('uri', self.playlist[self.playlist_index])
    self.player.set_state(gst.STATE_PLAYING)

  def on_message(self, bus, message):
    t = message.type
    if t == gst.MESSAGE_EOS:
      self.skip_forward()
    elif t == gst.MESSAGE_ERROR:
      self.skip_forward()
      #TODO: Log this!

Either I'm doing something wrong or there's a bug somewhere. Anyone got any ideas?

도움이 되었습니까?

해결책

Thanks to Alessandro Decina for giving me an answer on this one!

Basically, it wasn't working as add_signal_watch() adds a watch to the bus allowing GLib main loop to check the bus for new messages, which I hadn't implemented. Therefore to get this to work, I simply added:

gobject.MainLoop().run()

As an alternative to this I could have checked the bus for messages manually using:

bus.peek()

or

bus.poll(events, timeout)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top