我的目标是能够使Spotify应用程序而不是整个系统静音。使用命令:ps -C spotify -o pid=
我能够找到Spotify的进程ID,在这种情况下,该ID为"22981"
。使用该进程ID,我想从以下列表中进行搜索:pacmd list-sink-inputs
。该命令返回如下列表:
eric@eric-desktop:~$ pacmd list-sink-inputs
Welcome to PulseAudio! Use "help" for usage information.
>>> 1 sink input(s) available.
index: 0
driver: <protocol-native.c>
flags: START_CORKED
state: RUNNING
sink: 1 <alsa_output.pci-0000_00_1b.0.analog-stereo>
volume: 0: 100% 1: 100%
0: -0.00 dB 1: -0.00 dB
balance 0.00
muted: no
current latency: 1019.80 ms
requested latency: 371.52 ms
sample spec: s16le 2ch 44100Hz
channel map: front-left,front-right
Stereo
resample method: (null)
module: 8
client: 10 <Spotify>
properties:
media.role = "music"
media.name = "Spotify"
application.name = "Spotify"
native-protocol.peer = "UNIX socket client"
native-protocol.version = "26"
application.process.id = "22981"
application.process.user = "eric"
application.process.host = "eric-desktop"
application.process.binary = "spotify"
window.x11.display = ":0"
application.language = "en_US.UTF-8"
application.process.machine_id = "058c89ad77c15e1ce0dd5a7800000012"
application.process.session_id = "058c89ad77c15e1ce0dd5a7800000012-1345692739.486413-85297109"
application.icon_name = "spotify-linux-512x512"
module-stream-restore.id = "sink-input-by-media-role:music"
现在,我想将其application.process.id = "22981"
与接收器输入索引相关联,在这种情况下为index: 0
。现在,使用该索引号,我将需要运行以下命令:pacmd set-sink-input-mute 0 1
使Spotify静音并pacmd set-sink-input-mute 0 0
取消对Spotify的静音。对于这些命令,第一个数字将需要替换为先前找到的索引号,而下一个数字是用于打开或关闭静音的布尔值。我如何将其全部放入脚本中,以便获得命令以使Spotify应用程序静音/取消静音?
编辑:以下
两个脚本均按预期工作,有人可以添加一个切换按钮,该按钮将进行检查muted: yes
或muted: no
相应地静音或取消静音?
编辑: 我能够修改格伦·杰克曼的脚本来添加切换:
#!/bin/bash
main() {
local action=toggle
while getopts :mu option; do
case "$option" in
m) action=mute ;;
u) action=unmute ;;
?) usage 1 "invalid option: -$OPTARG" ;;
esac
done
shift $((OPTIND - 1))
local pid=$(pidof "$1")
if [[ -z "$pid" ]]; then
echo "error: no running processes for: $1" >&2
elif [[ "$1" ]]; then
$action "$1"
else
usage 1 "specify an application name"
fi
}
usage() {
[[ "$2" ]] && echo "error: $2"
echo "Usage: $0 [-m | -u] appname"
echo "Default: toggle mute"
echo "Arguments:"
echo "-m = mute application"
echo "-u = unmute application"
exit $1
}
toggle() {
local status=$(get_status "$1")
if [[ "$status" == "yes" ]]; then
unmute "$1"
elif [[ "$status" == "no" ]]; then
mute "$1"
fi
}
mute() { adjust_muteness "$1" 1; }
unmute() { adjust_muteness "$1" 0; }
adjust_muteness() {
local index=$(get_index "$1")
local status=$(get_status "$1")
[[ "$index" ]] && pacmd set-sink-input-mute "$index" $2 >/dev/null
}
get_index() {
local pid=$(pidof "$1")
pacmd list-sink-inputs |
awk -v pid=$pid '
$1 == "index:" {idx = $2}
$1 == "application.process.id" && $3 == "\"" pid "\"" {print idx; exit}
'
}
get_status() {
local pid=$(pidof "$1")
pacmd list-sink-inputs |
awk -v pid=$pid '
$1 == "muted:" {idx = $2}
$1 == "application.process.id" && $3 == "\"" pid "\"" {print idx; exit}
'
}
main "$@"
pactl list sink-inputs
?然后它将通过网络工作。