windows I简单音频音量G/设置静音未更新

brgchamk  于 2023-01-10  发布在  Windows
关注(0)|答案(1)|浏览(127)

解决方法:我引用了一个变量的指针,而不是变量本身。谢谢@RomanR。
我尝试通过ISimpleAudioVolume使用GetMute/SetMute,但我看到的内容有问题。当我运行GetMute时,我可以看到它没有静音(这很好),但当我将音频输出静音或运行SetMute(false)时,它无法更新任何内容。我不确定是否正确设置了AudioClient,但不知道如何检查。
谢谢!
编辑:我认为我的问题是在初始化链的顶部附近。当我运行check the volume时,它说它是100%,这是不正确的。当我检查设备ID和状态时,两个ID的开头是一串0,状态说它们都是不活动的。如果我检查AudioSessionControl,没有名称,但它说会话也是不活动的。
我猜它在init()堆栈中的某个地方:

check(CoInitialize(NULL));
check(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID*)&m_deviceEnumerator));
check(m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_device));

MainVolumeControl.h

#pragma once

#include <cstdio>
#include <windows.h>
#include <audioclient.h>
#include <audiopolicy.h>
#include <Mmdeviceapi.h>

class MainVolumeControl
{

public:
    MainVolumeControl();

    void init();

    void destroy();

    void toggleMute();

    void GetMute(BOOL* o_mute);

    IMMDeviceEnumerator* m_deviceEnumerator;
    IMMDevice* m_device;
    IAudioClient* m_audioClient;
    ISimpleAudioVolume* m_audioVolume;
    WAVEFORMATEX* m_pwfx;
};

MainVolumeControl.cpp

MainVolumeControl::MainVolumeControl()
{
    m_deviceEnumerator = nullptr;
    m_device = nullptr;
    m_audioClient = nullptr;
    m_audioVolume = nullptr;
    m_pwfx = nullptr;
}

void MainVolumeControl::init()
{
    // Get Device
    check(CoInitialize(NULL));
    check(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID*)&m_deviceEnumerator));
    check(m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_device));

    // Get Audio Client
    m_device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&m_audioClient);

    // Activate Audio Client & Get Service 
    check(m_audioClient->GetMixFormat(&m_pwfx));
    check(m_audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 10000000, 0, m_pwfx, NULL));
    check(m_audioClient->GetService(__uuidof(ISimpleAudioVolume), (void**)&m_audioVolume));
}

void MainVolumeControl::destroy()
{
    // Release the resources
    m_audioVolume->Release();
    m_audioClient->Release();
    m_device->Release();
    m_deviceEnumerator->Release();
    CoUninitialize();
}

void MainVolumeControl::toggleMute()
{
    BOOL mute = 0;

    // Getting the mute value
    check(m_audioVolume->GetMute(&mute));
    check(m_audioVolume->SetMute(!&mute, NULL));
}

void MainVolumeControl::GetMute(BOOL* o_mute)
{
    // Finally getting the mute value
    check(m_audioVolume->GetMute(o_mute));
}

Main.cpp

int main(int argc, CHAR* argv[])
{
    MainVolumeControl mvc;
    BOOL mute = 1;

    mvc.init();

    mvc.GetMute(&mute);
    printf("Mute state: %d\n", mute);
    printf("Toggling mute\n");
    mvc.toggleMute();
    mvc.GetMute(&mute);
    printf("Mute state: %d\n", mute);

    mvc.destroy();

    return 0;
}

产出

Mute state: 0
Toggling mute
Mute state: 0
5t7ly7z5

5t7ly7z51#

我引用了一个变量的指针而不是变量本身。谢谢@RomanR。
应为:

void MainVolumeControl::toggleMute()
{
    BOOL mute = 0;

    // Getting the mute value
    check(m_audioVolume->GetMute(&mute));
    check(m_audioVolume->SetMute(!mute, NULL));
}

相关问题