2012-08-28 78 views
0

此python程序將播放來自Mediapplayer的播放列表。當一首歌曲結束或在鍵盤上打字後,播放列表將播放下一首歌曲。在MediaPlayer中使用d-bus播放音樂列表

輸入是:

#!/usr/bin/python 
#-*-coding:ascii-*- 

import dbus 
import gobject 

from dbus.mainloop.glib import DBusGMainLoop 
DBusGMainLoop(set_as_default=True) 
bus = dbus.SessionBus() 

banshee = bus.get_object('org.mpris.MediaPlayer2.banshee', 
       '/org/mpris/MediaPlayer2') 
iface = dbus.Interface(banshee,'org.mpris.MediaPlayer2.Player') 

loop = gobject.MainLoop() 
def on_reply(): 
     print"Start" 
def on_error(): 
     print"Error" 
def next_song(): 
     print"Next Song" 
     iface.Next(reply_handler=on_reply, 
     error_handler=on_error) 
     gobject.timeout_add(4,next_song) 
def on_error(error): 
     print"Error" 
     loop.quit() 
     next_song() 
try: 
     loop.run() 
finally: 
     print"End" 
     iface.Stop() 

輸出: 沒有

預先感謝您

回答

1

路徑錯誤。正確的路徑是:

女妖= bus.get_object( 「org.bansheeproject.Banshee」, 「/組織/ bansheeproject /女妖/ PlayerEngine」)

在以下page你得到關於路徑更多的信息和怎麼運行的。

1

mdbus2應該給可用的方法列表。從this page摘自:

$ mdbus2 org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 
... 
[METHOD] org.mpris.MediaPlayer2.Player.Pause() ->() 
[METHOD] org.mpris.MediaPlayer2.Player.PlayPause() ->() 
[METHOD] org.mpris.MediaPlayer2.Player.Stop() ->() 
[METHOD] org.mpris.MediaPlayer2.Player.Play() ->() 
... 

調整的例子,以反映你與女妖接口的事實。我嘗試安裝Banshee和mdbus2來自己嘗試,但安裝程序腳本失敗。

您也可以嘗試使用Python的內置工具進行自省。

$ python 
>>> from org.mpris.MediaPlayer2 import Player 
>>> dir(Player) 

更好的是,在程序中放一個類似的語句。你的代碼體系現在已經足夠小了,你可以打印出dir()的結果。

finally: 
    print"End" 
    dir(iface) 
    iface.Stop() 
+0

非常感謝,但錯誤符合iface.Stop()error-print指示Stop()是未知方法,並且接口「org.mpris.MediaPlayer2.Player」不存在 – Studie

1

您可以使用d-feet來查看您嘗試使用的接口上是否有可用的方法。

您還可以使用dbus-monitor來查看在總線上傳遞的消息。語法應該是這樣的:

dbus-monitor --monitor --address <your_bus_address> 

您將通過上述命令獲取總線上的所有消息。要進行過濾,您可以執行如下操作:

dbus-monitor --monitor --address <your_bus_address> interface=<IF_name> path=<path_name> dest=... 

您不能在過濾中使用部分接口/路徑名稱。你總是可以grep進行一些高級過濾。

如果該方法在該接口上不可用,或者您嘗試使用錯誤的參數類型集來調用它,那麼通常會出現此錯誤。在您的調用代碼中也檢查函數簽名。

+0

嘗試過dbus-monitor並打印大量文本 – Studie

+0

默認情況下,它打印總線上發生的所有事情。您可以過濾郵件。我編輯了消息以顯示如何過濾。您應該使用您感興趣的接口進行過濾。 –

+0

這是print:signal sender = org.freedesktop.DBus - > dest = 1.166 serial = 2 path =/org/freedesktop/DBus;接口= org.freedesktop。的DBus; member = NameAcquired string「:1.166」 – Studie