Spotify Unity快速清单


10

如何为Spotify(从他们的网站安装)添加Unity“快速列表”,以便当我右键单击扩展坞上的图标时,我得到:“下一个,上一个,停止,播放”等等?

Answers:


5

spotify(本地linux客户端)

由于Spotify包含用于控制其某些功能的指示器,因此这使我们可以使用它dbus来发送事件。

ubuntuforums上有一个很棒的脚本可以解决这个问题。

首先安装先决条件:

sudo apt-get install libnet-dbus-perl

现在,将脚本复制并粘贴到一个名为的文本文件中spcmd,并将其保存到您的主文件夹中。

赋予它执行权:

chmod +x ~/spcmd

让我们将其移动到更有用的文件夹中:

mv ~/spcmd /usr/local/bin

现在,让我们创建一个快速列表。

首先将spotify桌面文件复制到您的主文件夹中:

mkdir -p ~/.local/share/applications
cp /usr/share/applications/spotify.desktop ~/.local/share/applications

打开文件,然后将快速列表复制并粘贴到文件末尾。保存。

gedit ~/.local/share/applications/spotify.desktop

最终结果

在此处输入图片说明

快速清单

X-Ayatana-Desktop-Shortcuts=PlayPause;Next;Previous;Stop;

[PlayPause Shortcut Group]
Name=PlayPause
Exec=spcmd playpause
TargetEnvironment=Unity

[Next Shortcut Group]
Name=Next
Exec=spcmd next
TargetEnvironment=Unity

[Previous Shortcut Group]
Name=Previous
Exec=spcmd previous
TargetEnvironment=Unity

[Stop Shortcut Group]
Name=Stop
Exec=spcmd stop
TargetEnvironment=Unity

spcmd

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use File::Basename;
use Net::DBus;

# Figure out some dbus stuff
unless ( defined $ENV{'DBUS_SESSION_BUS_ADDRESS'} ) {
&set_DBUS_SESSION_BUS_ADDRESS;
#die "Don't know which dbus to attach to.\nMake sure environment variable DBUS_SESSION_BUS_ADDRESS is set.";
}
#my $bus = Net::DBus->find;
my $bus = Net::DBus->session;
my $spotify = $bus->get_service("org.mpris.MediaPlayer2.spotify");
my $player = $spotify->get_object("/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player");
my $application = $spotify->get_object("/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2");
my $getorset = $spotify->get_object("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties");

# Handle command line argument
if (scalar @ARGV == 0) { &print_help; }
given ($ARGV[0]) {
# when ('play') { $player->Play(); } #Does not work for some reason.
when ('pause') { $player->Pause(); }
when ('playpause') { $player->PlayPause(); }
when ('next') { $player->Next(); }
when ('previous') { $player->Previous(); }
when ('stop') { $player->Stop(); }
when ('playstatus') { print $getorset->Get("org.mpris.MediaPlayer2.Player", "PlaybackStatus") . "\n"; }
when (m/\bspotify:(artist|album|track):[0-9a-zA-z]{22}\b/) { $player->OpenUri($_); }
when ('metadata-debug') { &print_debug_metadata; }
default { &print_help; }
}


# Print the help text
sub print_help {
print "USAGE: " . basename($0) . " {pause|playpause|next|previous|stop|playstatus|met adata-debug|<spotify URI>}\n\n";
print "\t" . "pause" . "\t\t" . "Causes spotify to pause current playback." . "\n";
print "\t" . "playpause" . "\t" . "Causes spotify to pause or play current playback." . "\n";
print "\t" . "next" . "\t\t" . "Goes to next song." . "\n";
print "\t" . "previous" . "\t" . "Goes to previous song." . "\n";
print "\t" . "stop" . "\t\t" . "Stops playback." . "\n";
print "\t" . "playstatus" . "\t" . "Prints current playstatus (Playing/Paused)." . "\n";
print "\t" . "<spotify URI>" . "\t" . "Starts playing supplied URI." . "\n";
print "\t" . "metadata-debug" . "\t" . "Shows available data on currently playing song." . "\n";
print "\t" . "\t\t" . "Fairly unformatted, thus \"debug\" data." . "\n";
print "\n";
print "EXAMPLES:\t" . basename($0) . " playpause" . "\n";
print "\t\t" . basename($0) . " spotify:track:5XXAq1r5r73ZyBS0XAiGw0" . "\n";

exit;
}

# Print some raw metadata
sub print_debug_metadata {
# Dereference the metadata hashref by copying it to a local hash
my %metadata = %{ $getorset->Get("org.mpris.MediaPlayer2.Player", "Metadata") };

# Print all metadata
print "Now Playing:\n";
for (keys %metadata) {
print $_ . ":\t" . $metadata{$_} . "\n" unless ($_ eq 'xesam:artist');
}

# Dereference the artist arrayref by copying it to local array
my @artistarray = @{ $metadata{'xesam:artist'} };

# Print all artists.
foreach my $artist (@artistarray) {
print "artist: \t" . $artist . "\n";
}
}

sub set_DBUS_SESSION_BUS_ADDRESS {
my $curruser = `whoami`; chomp $curruser;
my $procname = 'spotify';
my $pid = `pgrep -o -u $curruser $procname`; chomp $pid;
my $environ = '/proc/' . $pid . '/environ';
my $dbussession = `grep -z DBUS_SESSION_BUS_ADDRESS $environ`; $dbussession =~ s/^DBUS_SESSION_BUS_ADDRESS=//;

$ENV{'DBUS_SESSION_BUS_ADDRESS'} = $dbussession;
}

5

我认为到目前为止给出的答案有些复杂。不需要单独的脚本,可以通过直接发送相关的DBus命令dbus-send。只需确保dbus已安装该软件包,然后在命令行中发出以下命令:

mkdir -p ~/.local/share/applications
cp /usr/share/applications/spotify.desktop ~/.local/share/applications/

编辑文件~/.local/share/applications/spotify.desktop以读取:

[Desktop Entry]
Name=Spotify
GenericName=Music Player
Comment=Listen to music using Spotify
Icon=spotify-client
Exec=spotify %U
TryExec=spotify
Terminal=false
Type=Application
Categories=Qt;Audio;Music;Player;AudioVideo
MimeType=x-scheme-handler/spotify
# ====> MODIFICATIONS START HERE <=====
Actions=PlayPause;Next;Previous

[Desktop Action PlayPause]
Name=Play/Pause
Exec=dbus-send --print-reply=literal --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
OnlyShowIn=Messaging Menu;Unity;

[Desktop Action Next]
Name=Next
Exec=dbus-send --print-reply=literal --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
OnlyShowIn=Messaging Menu;Unity;

[Desktop Action Previous]
Name=Previous
Exec=dbus-send --print-reply=literal --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
OnlyShowIn=Messaging Menu;Unity;

这样就完成了。


这是最好的答案。但是,当我尝试手动执行此操作时,我错过了--print-reply=literal,但是它不起作用。你有解释吗?我对DBus几乎一无所知。
陶巴塔

很棒的答案:)另一个问题,是否也可以添加“竖起大拇指”和“竖起大拇指”按钮(spotify收音机中的按钮:))?
莱纳斯(Linus)

2

spotify_cmd是用于控制wine下正在运行的Spotify实例的工具 ,它也应在Windows上运行,但尚未经过测试。

下载spotifycmd。复制到桌面。然后

cd ~/Desktop/
tar -xvjf spotifycmd-0.5.tar.bz2 
sudo cp -r spotifycmd /usr/bin/

现在Exec=/usr/bin/spotifycmd/spotify_cmd.exe XXXX在创建快速列表时使用。

这里XXXXplaypausenextprevstopvoldownvolup,等。

有关创建快速列表的指南,请查看我的答案


这个答案很好!!!我在Spotify_cmd.exe工作时遇到问题。它Can not find spotify, is it running?在终端返回。这是正确的路线!
Ryan McClure 2012年

Oof,有本机的方法吗?
MarkovCh1 2012年

@Syzygy我没找到一个。:(
Rahul Virpara

hmmm-考虑到错误-也许您必须在wine下运行Spotify的Windows版本,此基于Wine的spotify_cmd才能正常工作。
fossfreedom

@fossfreedom你是对的。刚刚检查了源。它包括windows.h一个提供Win32 API的库。
拉胡尔·维尔帕拉

-1

Spotify将在面板上显示一个图标。只需单击该按钮,即可播放,停止,暂停,下一个等等(不记得全部)。不确定是否能回答您的问题。


2
这个问题是在询问如何将功能添加到Unity启动器中的Spotify图标中。
Eliah Kagan 2012年

好吧,只是我误解了他的问题。
user66987 2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.