使用 Dbus 更改 Spotify 音量

使用 Dbus 更改 Spotify 音量

我正在尝试创建一个 shell 脚本,可以控制在 ubuntu 上运行的 Spotify 客户端的音量(不是主音量)。但是我使用 Dbus 和命令时出现此错误$player->Volume(d)

.freedesktop.DBus.Error.InvalidArgs: Error setting property 'Volume': Expected type 'd' but got 's'

Shell 脚本:(这里仅使用值 0.2 进行测试)

#!/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 ('0.2') {$player->Volume(0.2);}
    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|metadata-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;
}

为什么它是不好的类型?还有其他解决方法吗?

提前致谢

答案1

我不知道您是否仍想解决这个问题,但是如果有人遇到这个问题:

创建以下脚本:

#!/bin/bash

spotify=$(pacmd list-sink-inputs | tr '\n' '\r' | perl -pe 's/ *index: ([0-9]+).+?application\.process\.binary = "([^\r]+)"\r.+?(?=index:|$)/\2 : \1\r/g' | tr '\r' '\n' | awk '/spotify/{print $3}')

step=2
pactl set-sink-input-volume $spotify +${step}%

首先,我们创建一个变量“spotify”,其中包含 Spotify 应用程序的接收器输入编号。然后,我们定义变量step=2来设置音量步骤。最后,我们用它pactl来增加音量。您可以用它-${step}%来降低音量。

相关内容