当我玩一些游戏,如骑士精神,中世纪战争,我切换到另一个软件,如火狐或桌面,游戏的声音仍然在播放。
那么,我该如何解决这个问题呢?
发布于 2016-06-12 16:43:17
如果某个特定应用程序的窗口不在
(自动)静音
我在Rhythmbox上测试了下面的脚本,完成了这个工作,没有一个错误。
在后台运行下面的脚本将暂停目标进程,如果它的窗口不在前面(在一秒钟内),如果它随应用程序一起出现,就会发出静音。
每当窗口再次出现在前面时,进程就会恢复到原来的位置,声音再次工作。
如果目标进程/应用程序根本不运行,则脚本切换到更长的时间(模式),如果目标应用程序是否运行,则每5秒只检查一次。这样,如果没有工作要完成的话,剧本的汁液就会非常少。
#!/usr/bin/env python3
import subprocess
import time
# ---set the proc to pause when not the active window
wclass = "rhythmbox"
# ---
def get(cmd):
# just a helper function to not repeat verbose subprocess sections
try:
return subprocess.check_output(cmd).decode("utf-8").strip()
except subprocess.CalledProcessError:
pass
while True:
# the longer period: application is not running (saving fuel)
time.sleep(5)
front1 = ""
while True:
# if the application runs, switch to a shorter response time
time.sleep(1)
# get the possible pid, get() returns "None" if not running,
# script then switches back to 5 sec check
pid = get(["pgrep", wclass])
if pid:
front2 = wclass in get([
"xprop", "-id", get(["xdotool", "getactivewindow"])
])
# run either kill -stop or kill -cont only if there is
# a change in the situation
if front2 != front1:
if front2 == True:
cm = ["kill", "-cont", pid]
print("run") # just a test indicator, remove afterwards
else:
cm = ["kill", "-stop", pid]
print("stop") # just a test indicator, remove afterwards
subprocess.Popen(cm)
front1 = front2
else:
break
xdotool
来获取最前端窗口的信息: sudo apt-get install xdotool。pause_app.py
rhythmbox
)。通常与WM_CLASS
的(第一部分)相同,但在您的情况下,我怀疑这不应该是steam
或其他什么东西。运行以确保ps -u 进行正确的猜测,然后终止 (进程id)以进行检查。该脚本可以很容易地编辑成多个应用程序,但首先请查看这是否是您所需要的。
如果您希望在目标窗口不在前面时通常会静音,请将命令替换为将应用程序暂停为静音(/unmute)声音。然后,脚本变成:
#!/usr/bin/env python3
import subprocess
import time
# ---set the proc to pause when not the active window
wclass = "rhythmbox"
# ---
def get(cmd):
# just a helper function to not repeat verbose subprocess sections
try:
return subprocess.check_output(cmd).decode("utf-8").strip()
except subprocess.CalledProcessError:
pass
while True:
# the longer period: application is not running (saving fuel)
time.sleep(5)
front1 = ""
while True:
# if the application runs, switch to a shorter response time
time.sleep(1)
# get the possible pid, get() returns "None" if not running,
# script then switches back to 5 sec check
pid = get(["pgrep", wclass])
if pid:
front2 = wclass in get([
"xprop", "-id", get(["xdotool", "getactivewindow"])
])
# run either kill -stop or kill -cont only if there is
# a change in the situation
if front2 != front1:
if front2 == True:
cm = ["amixer", "-q", "-D", "pulse", "sset", "Master", "on"]
print("sound") # just a test indicator, remove afterwards
else:
cm = ["amixer", "-q", "-D", "pulse", "sset", "Master", "off"]
print("no sound") # just a test indicator, remove afterwards
subprocess.Popen(cm)
front1 = front2
else:
break
用法与第一个脚本完全类似。
发布于 2016-06-12 12:50:13
如果游戏使用的是来自ubuntu的正常声音系统,即a。脉冲音频,然后进入:
系统设置->声音->应用程序
你应该看到你的歌曲播放应用程序,你可以改变音量,甚至静音。
https://askubuntu.com/questions/786055
复制相似问题