我正在尝试创建一个 Python 插件,它将在 Rhythmbox 2.96 中设置当前播放歌曲的评级。看来 Rhythmbox 2.96 不再允许您使用 API(Python 模块)来设置歌曲的评级;与玩家相关的操作已被取消,以支持 MPRIS。
然后我尝试查看将 dbus 与 MPRIS 结合使用,但 MPRIS 也没有设置歌曲评级的规范。经过大量挖掘,我找到了 this sample在 Rhythmbox 代码库中并将其改编为测试脚本。
它有效,但 SetEntryProperties 方法导致 Rhythmbox 卡住大约 30 秒。这是 Python 脚本。
说明:
将代码复制到名为 rate.py 的文件中
使用
从终端启动 rhythmboxrhythmbox -D rate
在 Rhythmbox 中,从插件启用 Python 控制台
启动 Python 控制台并运行
execfile('/path/to/rate.py')
您将在终端中看到打印输出,Rhythmbox 会卡住大约 20-30 秒。
# rhythmbox -D rate
# Rhythmbox: Edit > Plugins > Python Console enabled
# Play a song
# Open Rhythmbox Python Console
# execfile('/path/to/rate.py')
import sys
import rb
from gi.repository import Gtk, Gdk
def rateThread(rating):
try:
currentSongURI = shell.props.shell_player.get_playing_entry().get_playback_uri()
print "Setting rating for " + currentSongURI
from gi.repository import GLib, Gio
bus_type = Gio.BusType.SESSION
flags = 0
iface_info = None
print "Get Proxy"
proxy = Gio.DBusProxy.new_for_bus_sync(bus_type, flags, iface_info,
"org.gnome.Rhythmbox3",
"/org/gnome/Rhythmbox3/RhythmDB",
"org.gnome.Rhythmbox3.RhythmDB", None)
print "Got proxy"
rating = float(rating)
vrating = GLib.Variant("d", rating)
print "SetEntryProperties"
proxy.SetEntryProperties("(sa{sv})", currentSongURI, {"rating": vrating})
print "Done"
except:
print sys.exc_info()
return False
def rate():
if shell.props.shell_player.get_playing_entry():
Gdk.threads_add_idle(100, rateThread, 3)
rate()
打印的异常是:
Desktop/test2.py:41: (<class 'gi._glib.GError'>, GError('Timeout was
reached',), <traceback object at 0x913e554>)
我对 Python/dbus 的了解有限,所以我不明白为什么会出现该错误。如果有任何帮助,我将不胜感激。
此外,如果您知道通过代码在 Rhythmbox 中设置歌曲评级的更好方法,也欢迎您!
我正在使用 Ubuntu 12.04,如果它有所不同的话。
最佳答案
在插件中设置评级
Rhythmbox 2.9x 确实提供了一个 API 来设置评级 - 除非您使用外部程序(例如 Rhythmbox 托盘图标),否则无需通过 dbus 调用。
评级在其内部数据库中保存为 double 值。使用 RhythmDBEntry,您可以获得评级
rating = entry.get_double(RB.RhythmDBPropType.RATING)
要设置评级,您需要 RhythmDB entry_set 函数:
db=self.shell.props.db db.entry_set(entry, RB.RhythmDBPropType.RATING, rating)
获取和设置评级的示例代码可以在 CoverArt Browser 中找到插件(coverart_album.py)
关于python - 如何设置在 Rhythmbox 2.96 中播放的歌曲的评级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568599/