首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当没有焦点时,我如何自动静音应用程序?

当没有焦点时,我如何自动静音应用程序?
EN

Ask Ubuntu用户
提问于 2016-06-12 11:48:49
回答 2查看 4.8K关注 0票数 3

当我玩一些游戏,如骑士精神,中世纪战争,我切换到另一个软件,如火狐或桌面,游戏的声音仍然在播放。

那么,我该如何解决这个问题呢?

EN

回答 2

Ask Ubuntu用户

回答已采纳

发布于 2016-06-12 16:43:17

如果某个特定应用程序的窗口不在

前面,则该应用程序的

(自动)静音

我在Rhythmbox上测试了下面的脚本,完成了这个工作,没有一个错误。

在后台运行下面的脚本将暂停目标进程,如果它的窗口不在前面(在一秒钟内),如果它随应用程序一起出现,就会发出静音。

每当窗口再次出现在前面时,进程就会恢复到原来的位置,声音再次工作。

如果目标进程/应用程序根本不运行,则脚本切换到更长的时间(模式),如果目标应用程序是否运行,则每5秒只检查一次。这样,如果没有工作要完成的话,剧本的汁液就会非常少。

脚本

代码语言:javascript
运行
复制
#!/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
  • 在脚本的head部分,将进程名设置为pause (替换rhythmbox)。通常与WM_CLASS的(第一部分)相同,但在您的情况下,我怀疑这不应该是steam或其他什么东西。运行以确保ps -u 进行正确的猜测,然后终止 (进程id)以进行检查。
  • 通过命令python3 /path/to/path_app.py运行脚本,并检查是否所有操作都如预期的那样。
  • 如果一切正常,添加到启动应用程序:破折号>启动应用程序>添加。然后添加命令: python3 /path/to/pause_app.py

Note

该脚本可以很容易地编辑成多个应用程序,但首先请查看这是否是您所需要的。

Alternatively

如果您希望在目标窗口不在前面时通常会静音,请将命令替换为将应用程序暂停为静音(/unmute)声音。然后,脚本变成:

代码语言:javascript
运行
复制
#!/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

用法与第一个脚本完全类似。

票数 5
EN

Ask Ubuntu用户

发布于 2016-06-12 12:50:13

如果游戏使用的是来自ubuntu的正常声音系统,即a。脉冲音频,然后进入:

系统设置->声音->应用程序

你应该看到你的歌曲播放应用程序,你可以改变音量,甚至静音。

票数 3
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/786055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档