控制其它应用程序的卷 [英] Controlling the volume of other applications

查看:155
本文介绍了控制其它应用程序的卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让控制使用Windows 7 API音频另一个进程的音量一个应用程序。

I am trying to make an app that controls the volume of another process using the Windows 7 Audio API.

我要找的是 ISimpleAudioVolume < /一>用于由其它方法中使用的会话

What I'm looking for is the ISimpleAudioVolume for the session used by the other process.

我已经使用 IAudioSessionEnumerator 但它只会给我 IAudioSessionControl2 的会话。使用IAudioSessionControl我成功当我改变音量通过sndvol但不能将自己收到通知。

I have tried using the IAudioSessionEnumerator but it will only give me the IAudioSessionControl2 of the session. Using the IAudioSessionControl I have managed to receive notifications when I change the volume through sndvol but not change it myself.

我一直在使用也尝试 GetSimpleAudioVolume()从IAudioSessionManager但它只会给我在当前进程中的会话。

I have also tried using GetSimpleAudioVolume() from IAudioSessionManager but it will only give me sessions within the current process.

你是怎么做到的呢?它应该是可能的,因为sndvol是这样做的。

How do you do it? It should be possible since sndvol is doing this.

推荐答案

下面是使用静音核心音频API另一个进程的一个例子。

Here is an example of muting another process using Core Audio API.

#include <windows.h>
#include <iostream>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <Audiopolicy.h>
#include <comdef.h>
#include <comip.h>

#define CHECK_HR(hr)   \
    if(FAILED(hr)) {    \
        std::cout << "error" << std::endl;   \
        return 0;   \
                }
_COM_SMARTPTR_TYPEDEF(IMMDevice, __uuidof(IMMDevice));
_COM_SMARTPTR_TYPEDEF(IMMDeviceEnumerator, __uuidof(IMMDeviceEnumerator));
_COM_SMARTPTR_TYPEDEF(IAudioSessionManager2, __uuidof(IAudioSessionManager2));
_COM_SMARTPTR_TYPEDEF(IAudioSessionManager2, __uuidof(IAudioSessionManager2));
_COM_SMARTPTR_TYPEDEF(IAudioSessionEnumerator, __uuidof(IAudioSessionEnumerator));
_COM_SMARTPTR_TYPEDEF(IAudioSessionControl2, __uuidof(IAudioSessionControl2));
_COM_SMARTPTR_TYPEDEF(IAudioSessionControl, __uuidof(IAudioSessionControl));
_COM_SMARTPTR_TYPEDEF(ISimpleAudioVolume, __uuidof(ISimpleAudioVolume));

IAudioSessionManager2Ptr CreateSessionManager()
{
    HRESULT hr = S_OK;

    IMMDevicePtr pDevice;
    IMMDeviceEnumeratorPtr pEnumerator;
    IAudioSessionManager2Ptr pSessionManager;


    // Create the device enumerator.
    CHECK_HR(hr = CoCreateInstance(
        __uuidof(MMDeviceEnumerator),
        NULL, CLSCTX_ALL,
        __uuidof(IMMDeviceEnumerator),
        (void**)&pEnumerator));

    // Get the default audio device.
    CHECK_HR(hr = pEnumerator->GetDefaultAudioEndpoint(
        eRender, eConsole, &pDevice));

    // Get the session manager.
    CHECK_HR(hr = pDevice->Activate(
        __uuidof(IAudioSessionManager2), CLSCTX_ALL,
        NULL, (void**)&pSessionManager));

    return pSessionManager;
}

bool MuteProcess(DWORD processId) {

    IAudioSessionManager2Ptr mgr = CreateSessionManager();
    if (!mgr) {
        return false;
    }
    IAudioSessionEnumeratorPtr enumerator;
    if (SUCCEEDED(mgr->GetSessionEnumerator(&enumerator))) {
        int sessionCount;
        if (SUCCEEDED(enumerator->GetCount(&sessionCount))) {
            for (int i = 0; i < sessionCount; i++) {
                IAudioSessionControlPtr control;
                if (SUCCEEDED(enumerator->GetSession(i, &control))) {
                    IAudioSessionControl2Ptr control2;
                    if (SUCCEEDED(control->QueryInterface(__uuidof(IAudioSessionControl2), (void**)&control2))) {
                        DWORD foundProcessId;
                        if (SUCCEEDED(control2->GetProcessId(&foundProcessId))) {
                            if (foundProcessId == processId) {
                                ISimpleAudioVolumePtr volume;
                                if (SUCCEEDED(control2->QueryInterface(_uuidof(ISimpleAudioVolume), (void**)&volume))) {
                                    if (SUCCEEDED(volume->SetMute(TRUE, 0))) {
                                        return true;
                                    }
                                }
                            }
                        }

                    }

                }
            }
        }
    }
    return false;
}

int _tmain(int argc, _TCHAR* argv[]){
    CoInitialize(NULL);
    DWORD processId = 11944;
    MuteProcess(processId);

    return 0;
}

这篇关于控制其它应用程序的卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆