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

# CreateRenderAudioStateMonitor

> CreateRenderAudioStateMonitor

# CreateRenderAudioStateMonitor

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

创建 [IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) 的新实例,用于监视所有音频呈现流的音频级别。

## Syntax

```cpp theme={null}
HRESULT WINAPI CreateRenderAudioStateMonitor(
  _Out_ IAudioStateMonitor **audioStateMonitor
);
```

### Parameters

*audioStateMonitor* \_Out\_\
Type: IAudioStateMonitor\*\*

此输出参数将填充所创建的 `IAudioStateMonitor` 对象。

### Return value

Type: HRESULT

如果方法成功,则返回 `S\_OK`。

## Remarks

这是用于创建并返回 `AudioStateMonitor` 实例的其中一个函数。

对于捕获流和呈现流,各有四个变体,用于确定进程是否可以针对给定角色执行以下操作之一:

* 捕获或呈现任意类别的任意音频
* 捕获或呈现特定类别的音频
* 将特定类别的音频捕获或呈现到特定终结点。\
  终结点可以通过 `MMDevice` ID(使用 `IMMDevice::GetId()` 获取)指定,也可以通过其 `SWD` ID(使用 `Windows.Devices.Enumeration` 或 `Windows.Media.Devices.MediaDevice` 获取)指定
* 将特定类别的音频捕获或呈现到默认终结点

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

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

## See also

[AUDIO\_STREAM\_CATEGORY](https://learn.microsoft.com/windows/win32/api/audiosessiontypes/ne-audiosessiontypes-audio_stream_category)

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

[WASAPI](/build/console-features/audio/overviews/wasapi-overview)


## Related topics

- [IAudioStateMonitor 接口](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor.md)
- [AudioStateMonitor](/zh-CN/reference/audio/audiostatemonitor/audiostatemonitor_members.md)
- [CreateRenderAudioStateMonitorForCategory](/zh-CN/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitorforcategory.md)
- [CreateRenderAudioStateMonitorForCategoryAndDeviceRole](/zh-CN/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitorforcategoryanddevicerole.md)
- [CreateRenderAudioStateMonitorForCategoryAndDeviceId](/zh-CN/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitorforcategoryanddeviceid.md)
