[Gustavo Carneiro] handle stop music in music_track_listener.py. Fixes #564
This commit is contained in:
parent
382b245062
commit
4e1d7a21c9
|
@ -24,8 +24,9 @@ class MusicTrackInfo(object):
|
||||||
|
|
||||||
|
|
||||||
class MusicTrackListener(gobject.GObject):
|
class MusicTrackListener(gobject.GObject):
|
||||||
__gsignals__ = { 'music-track-changed': (gobject.SIGNAL_RUN_LAST, None,
|
__gsignals__ = {
|
||||||
(object,)) }
|
'music-track-changed': (gobject.SIGNAL_RUN_LAST, None, (object,)),
|
||||||
|
}
|
||||||
|
|
||||||
_instance = None
|
_instance = None
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -36,14 +37,39 @@ class MusicTrackListener(gobject.GObject):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(MusicTrackListener, self).__init__()
|
super(MusicTrackListener, self).__init__()
|
||||||
|
self._last_playing_music = None
|
||||||
|
|
||||||
bus = dbus.SessionBus()
|
bus = dbus.SessionBus()
|
||||||
|
|
||||||
|
## Muine
|
||||||
bus.add_signal_receiver(self._muine_music_track_change_cb, 'SongChanged',
|
bus.add_signal_receiver(self._muine_music_track_change_cb, 'SongChanged',
|
||||||
'org.gnome.Muine.Player')
|
'org.gnome.Muine.Player')
|
||||||
|
bus.add_signal_receiver(self._player_name_owner_changed,
|
||||||
|
'NameOwnerChanged', 'org.freedesktop.DBus', arg0='org.gnome.Muine')
|
||||||
|
bus.add_signal_receiver(self._player_playing_changed_cb, 'StateChanged',
|
||||||
|
'org.gnome.Muine.Player')
|
||||||
|
|
||||||
|
## Rhythmbox
|
||||||
bus.add_signal_receiver(self._rhythmbox_music_track_change_cb,
|
bus.add_signal_receiver(self._rhythmbox_music_track_change_cb,
|
||||||
'playingUriChanged', 'org.gnome.Rhythmbox.Player')
|
'playingUriChanged', 'org.gnome.Rhythmbox.Player')
|
||||||
|
bus.add_signal_receiver(self._player_name_owner_changed,
|
||||||
|
'NameOwnerChanged', 'org.freedesktop.DBus', arg0='org.gnome.Rhythmbox')
|
||||||
|
bus.add_signal_receiver(self._player_playing_changed_cb,
|
||||||
|
'playingChanged', 'org.gnome.Rhythmbox.Player')
|
||||||
|
|
||||||
|
def _player_name_owner_changed(self, name, old, new):
|
||||||
|
if not new:
|
||||||
|
self.emit('music-track-changed', None)
|
||||||
|
|
||||||
|
def _player_playing_changed_cb(self, playing):
|
||||||
|
if playing:
|
||||||
|
self.emit('music-track-changed', self._last_playing_music)
|
||||||
|
else:
|
||||||
|
self.emit('music-track-changed', None)
|
||||||
|
|
||||||
def _muine_properties_extract(self, song_string):
|
def _muine_properties_extract(self, song_string):
|
||||||
d = dict((x.strip() for x in s1.split(':', 1)) for s1 in song_string.split('\n'))
|
d = dict((x.strip() for x in s1.split(':', 1)) for s1 in \
|
||||||
|
song_string.split('\n'))
|
||||||
info = MusicTrackInfo()
|
info = MusicTrackInfo()
|
||||||
info.title = d['title']
|
info.title = d['title']
|
||||||
info.album = d['album']
|
info.album = d['album']
|
||||||
|
@ -67,7 +93,8 @@ class MusicTrackListener(gobject.GObject):
|
||||||
|
|
||||||
def _rhythmbox_music_track_change_cb(self, uri):
|
def _rhythmbox_music_track_change_cb(self, uri):
|
||||||
bus = dbus.SessionBus()
|
bus = dbus.SessionBus()
|
||||||
rbshellobj = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
|
rbshellobj = bus.get_object('org.gnome.Rhythmbox',
|
||||||
|
'/org/gnome/Rhythmbox/Shell')
|
||||||
rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell')
|
rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell')
|
||||||
props = rbshell.getSongProperties(uri)
|
props = rbshell.getSongProperties(uri)
|
||||||
info = self._rhythmbox_properties_extract(props)
|
info = self._rhythmbox_properties_extract(props)
|
||||||
|
@ -87,19 +114,22 @@ class MusicTrackListener(gobject.GObject):
|
||||||
if player.GetPlaying():
|
if player.GetPlaying():
|
||||||
song_string = player.GetCurrentSong()
|
song_string = player.GetCurrentSong()
|
||||||
song = self._muine_properties_extract(song_string)
|
song = self._muine_properties_extract(song_string)
|
||||||
|
self._last_playing_music = song
|
||||||
return song
|
return song
|
||||||
|
|
||||||
## Check Rhythmbox playing song
|
## Check Rhythmbox playing song
|
||||||
if dbus.dbus_bindings.bus_name_has_owner(bus.get_connection(),
|
if dbus.dbus_bindings.bus_name_has_owner(bus.get_connection(),
|
||||||
'org.gnome.Rhythmbox'):
|
'org.gnome.Rhythmbox'):
|
||||||
rbshellobj = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
|
rbshellobj = bus.get_object('org.gnome.Rhythmbox',
|
||||||
|
'/org/gnome/Rhythmbox/Shell')
|
||||||
player = dbus.Interface(
|
player = dbus.Interface(
|
||||||
bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player'),
|
bus.get_object('org.gnome.Rhythmbox',
|
||||||
'org.gnome.Rhythmbox.Player')
|
'/org/gnome/Rhythmbox/Player'), 'org.gnome.Rhythmbox.Player')
|
||||||
rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell')
|
rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell')
|
||||||
uri = player.getPlayingUri()
|
uri = player.getPlayingUri()
|
||||||
props = rbshell.getSongProperties(uri)
|
props = rbshell.getSongProperties(uri)
|
||||||
info = self._rhythmbox_properties_extract(props)
|
info = self._rhythmbox_properties_extract(props)
|
||||||
|
self._last_playing_music = info
|
||||||
return info
|
return info
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
@ -107,7 +137,10 @@ class MusicTrackListener(gobject.GObject):
|
||||||
# here we test :)
|
# here we test :)
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
def music_track_change_cb(listener, music_track_info):
|
def music_track_change_cb(listener, music_track_info):
|
||||||
print music_track_info.title
|
if music_track_info is None:
|
||||||
|
print "Stop!"
|
||||||
|
else:
|
||||||
|
print music_track_info.title
|
||||||
listener = MusicTrackListener.get()
|
listener = MusicTrackListener.get()
|
||||||
listener.connect('music-track-changed', music_track_change_cb)
|
listener.connect('music-track-changed', music_track_change_cb)
|
||||||
track = listener.get_playing_track()
|
track = listener.get_playing_track()
|
||||||
|
|
Loading…
Reference in New Issue