How can I determine which Windows process is the “audio session” governing the current process's output?

不打扰是莪最后的温柔 提交于 2019-12-11 15:46:15

问题


I happen to be using Python bindings here, but I suspect that the problem, and the eventual answer, are not Python-specific.

On Windows 10, using Python bindings to Windows' Core Audio library, specifically via pycaw.AudioUtilities.GetAllSessions(), I can see that there is a session with name 'conhost.exe' and pid 11512, and by elementary guesswork/experimentation I can see that this is the session that (a) governs the audio for my current process and (b) corresponds to the "Console Window Host" slider in the Windows 10 Volume Mixer. This is the one I want to manipulate from my program, and pycaw provides the bindings to do that.

So far so good. My problem is that I don't see any way of working backwards from the current process ID so that, on the next launch, my program can ask "which process corresponds to the session that governs my audio output?" Naively I expected that that session pid might appear in the current process's ancestry, but it does not:

import os, psutil

psutil.Process(os.getpid())
# prints: psutil.Process(pid=3628, name='python.exe', started='14:56:41')

psutil.Process(os.getpid()).parent()
# prints: psutil.Process(pid=16676, name='cmd.exe', started='13:00:57')

psutil.Process(os.getpid()).parent().parent()
# prints: psutil.Process(pid=1356, name='explorer.exe', started='12:21:26')

psutil.Process(os.getpid()).parent().parent().parent()
# prints nothing. It's `None`. We've reached the top.

How should I be querying the pid for whichever audio session governs the current process? (I presume the session won't always be named 'conhost.exe—sometimes I run pythonw.exe to execute the same code without a console.)

来源:https://stackoverflow.com/questions/57777770/how-can-i-determine-which-windows-process-is-the-audio-session-governing-the-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!