> ## 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.

# AudioStateMonitorSoundLevel 枚举

> AudioStateMonitorSoundLevel 枚举

# AudioStateMonitorSoundLevel 枚举

指定捕获流或呈现流的音量级别。

## Syntax

```cpp theme={null}
typedef enum _AudioStateMonitorSoundLevel { 
  Full = 0,
  Low = 1,
  Muted = 2
} AudioStateMonitorSoundLevel;
```

## Constants

| 常量    | 说明       |
| ----- | -------- |
| Full  | 音量处于最大值。 |
| Low   | 音量较低。    |
| Muted | 音量已静音。   |

## Remarks

此枚举由 [IAudioStateMonitor::GetSoundLevel](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel) 方法使用。

请思考以下场景:游戏仅在明确知道操作系统不会因策略而将音频静音时,才选择播放 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. 
} 
```

## Requirements

**头文件:** Audiostatemonitorapi.h

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

## See also

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


## Related topics

- [IAudioStateMonitor::GetSoundLevel 方法](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel.md)
- [AudioStateMonitor](/zh-CN/reference/audio/audiostatemonitor/audiostatemonitor_members.md)
- [IAudioStateMonitor::RegisterCallback 方法](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback.md)
- [IAudioStateMonitor::UnregisterCallback 方法](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback.md)
- [CreateRenderAudioStateMonitor](/zh-CN/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitor.md)
