> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# IAudioStateMonitor::GetSoundLevel 方法

> IAudioStateMonitor::GetSoundLevel 方法

# IAudioStateMonitor::GetSoundLevel 方法

\[部分信息涉及预发布产品,在其正式发布之前可能会发生重大更改。对于此处提供的信息,Microsoft 不作任何明示或默示的担保。]

获取与 [IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) 关联的流的当前音量级别。

## Syntax

```cpp theme={null}
AudioStateMonitorSoundLevel GetSoundLevel();
```

### Parameters

此方法没有参数。

### Return value

[AudioStateMonitorSoundLevel](/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel) 枚举中的一个值,指示与 [IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) 关联的流的当前音量级别。

## Remarks

请思考以下场景:游戏仅在明确知道操作系统不会因策略而将音频静音时,才选择播放 GameMedia 音频。为此,游戏在默认主机终结点上为 [AudioCategory\_GameMedia](https://learn.microsoft.com/windows/desktop/api/audiosessiontypes/ne-audiosessiontypes-audio_stream_category) 流类别注册 SoundLevel 通知。如果此类别的 SoundLevel 被设置为 “Muted”,游戏将停止播放其 GameMedia 流;如果 SoundLevel 是其他任何值,游戏将选择播放其 GameMedia 流。

```cpp theme={null}
// global variables 
winrt::com_ptr<IAudioStateMonitor> g_audioStateMonitor; 
AudioStateMonitorRegistrationHandle g_registration = 0; 

// Returns true if the GameMedia stream should be disabled, false if it should be 
// enabled. 
bool IsSoundLevelMuted(_In_ IAudioStateMonitor* audioStateMonitor) 
{ 
    return (audioStateMonitor->GetSoundLevel() == AudioStateMonitorSoundLevel::Muted); 
} 

HRESULT StartTrackingSoundLevel() 
{ 
    // Create an AudioStateMonitor and register for callbacks from it. 
    HRESULT hr = RegisterForSoundLevelChanges(); 
    if (SUCCEEDED(hr)) 
    { 
        // Check the current sound level and determine if the GameMedia stream should 
        // be enabled. 
        bool disableGameMediaStream = IsSoundLevelMuted(g_audioStateMonitor.get()); 

        // Add code here that enables or disables the GameMedia stream based on the 
        // value of the boolean. 
    } 
    return hr; 
} 

HRESULT RegisterForSoundLevelChanges() 
{ 
    HRESULT hr = S_OK; 

    // Create a new AudioStateMonitor for the GameMedia category, if needed, and 
    // register for callbacks. 
    if (m_audioStateMonitor == nullptr) 
    { 
        hr = CreateRenderAudioStateMonitorForCategoryAndDeviceRole( 
            AudioCategory_GameMedia, ERole::eConsole, g_audioStateMonitor.put()); 
        if (SUCCEEDED(hr)) 
        { 
            // Optional "context" parameter is not used in this example, 
            // and is set to nullptr. 
            hr = g_audioStateMonitor->RegisterCallback(OnAudioStateMonitorCallback, nullptr, &g_registration); 
        } 
    } 

    if (FAILED(hr)) 
    { 
        // g_audioStateMonitor is a smart pointer, so if an IAudioStateMonitor was 
        // allocated, setting the smart pointer to null invokes the Release method 
        // which will decrement its usage count and ensure that the object is properly 
        // destroyed. 
        g_audioStateMonitor = nullptr; 
    } 
    return hr; 
} 

// Unregister callbacks on program shutdown 
void UnregisterForSoundLevelChanges() 
{ 
    if (g_audioStateMonitor != nullptr) 
    { 
        if (g_registration) 
        { 
            g_audioStateMonitor->UnregisterCallback(g_registration); 
        } 

        // g_audioStateMonitor is a smart pointer, so setting it to null 
        // invokes the Release method to decrement its usage count. 
        g_audioStateMonitor = nullptr; 
    } 
} 

void OnAudioStateMonitorCallback(_In_ IAudioStateMonitor* audioStateMonitor, _In_opt_ void* context) 
{ 
    bool disableGameMediaStream = IsSoundLevelMuted(audioStateMonitor); 

    // Add code here that enables or disables the GameMedia stream based on the value 
    // of the boolean. 
} 
```

[AudioStateMonitorSoundLevel](/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel) 枚举中的一个值。指示与 [IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) 关联的流的当前音量级别。

## Requirements

**头文件:** Audiostatemonitorapi.h

**支持的平台:** Windows、XBOX One 系列主机和 XBOX Series 主机

## See also

[IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor)

[关于 WASAPI](https://learn.microsoft.com/windows/desktop/CoreAudio/wasapi)


## Related topics

- [AudioStateMonitorSoundLevel 枚举](/zh-CN/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel.md)
- [IAudioStateMonitor::RegisterCallback 方法](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback.md)
- [IAudioStateMonitor::UnregisterCallback 方法](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback.md)
- [IAudioStateMonitor 接口](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor.md)
- [CreateCaptureAudioStateMonitor](/zh-CN/reference/audio/audiostatemonitor/functions/createcaptureaudiostatemonitor.md)
