4

Pythonを使用してRhythmboxのトラックの変更を監視したいと思います。トラックの変更を継続的にチェックし、トラックが変更された場合は一連の機能を実行したいと思います。dbusからRhythmboxインターフェースを取得し、現在のトラックの詳細を取得するコードを作成しました。ただし、このプログラムは、変更を確認するために手動で実行する必要があります。

私はこれに不慣れで、Rhythmboxを継続的に実行およびチェックするバックグラウンドプロセスを作成する方法を知りたいです。

複数の音楽プレーヤーを聴くようにアプリケーションを拡張するので、Rhythmboxプラグインを作成したくありません(これにより、作業が簡単になります)。

機能を実現するために私がしなければならないことを正確に教えてください。

4

4 に答える 4

13

Rhythmbox プレーヤー オブジェクト ( /org/gnome/Rhythmbox/Player)playingUriChangedは、現在の曲が変わるたびに信号を送信します。関数を信号に接続して、信号が受信されるたびに実行されるようにします。次の例では、新しい曲が始まるたびに曲のタイトルを出力し、GLib メイン ループを使用して DBus メッセージを処理します。

#! /usr/bin/env python

import dbus
import dbus.mainloop.glib
import glib

# This gets called whenever Rhythmbox sends the playingUriChanged signal
def playing_song_changed (uri):
    global shell
    if uri != "":
        song = shell.getSongProperties (uri)
        print "Now playing: {0}".format (song["title"])
    else:
        print "Not playing anything"

dbus.mainloop.glib.DBusGMainLoop (set_as_default = True)

bus = dbus.SessionBus ()

proxy = bus.get_object ("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player")
player = dbus.Interface (proxy, "org.gnome.Rhythmbox.Player")
player.connect_to_signal ("playingUriChanged", playing_song_changed)

proxy = bus.get_object ("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Shell")
shell = dbus.Interface (proxy, "org.gnome.Rhythmbox.Shell")

# Run the GLib event loop to process DBus signals as they arrive
mainloop = glib.MainLoop ()
mainloop.run ()
于 2010-11-06T03:20:39.137 に答える
1

ここでConkyスクリプトを見てください:

https://launchpad.net/~conkyhardcore/+archive/ppa/+files/conkyrhythmbox_2.12.tar.gz

次のように、dbusを使用してrhythmboxと通信します。

bus = dbus.SessionBus()
remote_object_shell = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
iface_shell = dbus.Interface(remote_object_shell, 'org.gnome.Rhythmbox.Shell')
remote_object_player = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
iface_player = dbus.Interface(remote_object_player, 'org.gnome.Rhythmbox.Player')

iface_playerでいくつかの関数を呼び出して、必要な情報を取得できます。ただし、この例からポーリングする必要があるようです。トラック変更時にdbusからメッセージを受信したい場合は、別の方法で行う必要があります。これは、探索する手段から説明します。

http://ubuntuforums.org/showthread.php?t=156706

于 2010-10-12T23:58:14.427 に答える
1

私は Ubuntu 14.04.1 を使用していますが、上記のスクリプトは Rhythmbox 3 では非推奨です。このスクリプトを使用して現在の曲を ~/.now_playing に書き込み、BUTT が読み取れるようにしていますが、必要に応じて更新できます。Rhythmbox は現在 MPRIS を使用しており、ここで情報を取得できます。

http://specifications.freedesktop.org/mpris-spec/latest/index.html

#!/usr/bin/python

import dbus
import dbus.mainloop.glib
import glib

# This gets called whenever Rhythmbox sends the playingUriChanged signal
def playing_song_changed (Player,two,three):
    global iface
    global track
    global home
    track2 = iface.Get(Player,"Metadata").get(dbus.String(u'xesam:artist'))[0] + " - "+ iface.Get(Player,"Metadata").get(dbus.String(u'xesam:title'))

    if track != track2:
        track = iface.Get(Player,"Metadata").get(dbus.String(u'xesam:artist'))[0] + " - "+ iface.Get(Player,"Metadata").get(dbus.String(u'xesam:title'))
        f = open( home + '/.now_playing', 'w' )
        f.write( track + '\n' )
        f.close()


dbus.mainloop.glib.DBusGMainLoop (set_as_default = True)

bus = dbus.SessionBus ()
from os.path import expanduser
home = expanduser("~")
player = bus.get_object ("org.mpris.MediaPlayer2.rhythmbox", "/org/mpris/MediaPlayer2")
iface = dbus.Interface (player, "org.freedesktop.DBus.Properties")

track = iface.Get("org.mpris.MediaPlayer2.Player","Metadata").get(dbus.String(u'xesam:artist'))[0] + " - "+ iface.Get("org.mpris.MediaPlayer2.Player","Metadata").get(dbus.Strin$
f = open( home + "/.now_playing", 'w' )
f.write( track + '\n' )
f.close()

iface.connect_to_signal ("PropertiesChanged", playing_song_changed)

# Run the GLib event loop to process DBus signals as they arrive
mainloop = glib.MainLoop ()
mainloop.run ()
于 2014-12-08T07:03:07.000 に答える
-2

何かのようなもの:

from time import sleep

execute = True
while execute:
    your_function_call()
    sleep(30) # in seconds; prevent busy polling

うまくいくはずです。誰かがアプリケーションを ctrl-c したときに execute を False に設定できるように、シグナル ( ) をリッスンする何かにそれが接続されていれば、それはimport signal基本的にあなたが求めているものです。

それ以外の場合は、デーモン化用の Google を用意してください (プロセスを数回フォークする必要があります)。メモリから、まともなPythonライブラリさえあります(メモリから、2.5 / 2.6withステートメントが必要です)。これは、物事の側面をより簡単にするのに役立ちます:)。

于 2010-10-12T23:13:10.033 に答える