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

# Méthode IAudioStateMonitor::RegisterCallback

> Méthode IAudioStateMonitor::RegisterCallback

# Méthode IAudioStateMonitor::RegisterCallback

Inscrit une fonction de rappel que le système appellera lorsque le niveau audio des flux associés à [IAudioStateMonitor](/fr-CA/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) change.

## Syntaxe

```cpp theme={null}
HRESULT RegisterCallback(
  AudioStateMonitorCallback *callback,
  void *context,
  [out] AudioStateMonitorRegistration *registration
);
```

### Paramètres

*callback*\
Type : AudioStateMonitorCallback\*

Pointeur vers la fonction de rappel appelée par le système lorsque le niveau audio des flux associés à [IAudioStateMonitor](/fr-CA/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) change.

*context*\
Type : void\*

Pointeur vers les données de contexte définies par l'application qui seront passées à la fonction spécifiée dans le paramètre *callback*.

*registration* \[out]\
Type : AudioStateMonitorRegistration\*

Entier 64 bits qui identifie l'inscription du rappel. Pour désinscrire la fonction spécifiée dans le paramètre *callback*, passez cette valeur à [IAudioStateMonitor::UnregisterCallback](/fr-CA/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback).

### Valeur de retour

Type : HRESULT

Si la méthode réussit, elle retourne S\_OK. Si elle échoue, les codes de retour possibles comprennent, sans s'y limiter, les valeurs indiquées dans le tableau suivant.

| Code de retour | Description |
| - | - |
| <dl> <dt> **E\_OUTOFMEMORY**</dt> </dl> | Le pointeur de rappel fourni est nul.<br /> |

## Remarques

Considérez le scénario suivant, dans lequel un jeu choisit de lire de l'audio GameMedia uniquement lorsqu'il est connu que l'audio ne sera pas mis en sourdine par le système d'exploitation en raison d'une stratégie. Pour ce faire, le jeu s'inscrit aux notifications SoundLevel pour la catégorie de flux [AudioCategory\_GameMedia](https://learn.microsoft.com/windows/desktop/api/audiosessiontypes/ne-audiosessiontypes-audio_stream_category) sur le point de terminaison de console par défaut. Si le SoundLevel de cette catégorie est réglé à 'Muted', le jeu arrêtera la lecture de son flux GameMedia. Si le SoundLevel est réglé à toute autre valeur, le jeu choisit de lire son flux 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. 
} 
```

## Configuration requise

**En-tête :** Audiostatemonitorapi.h

**Plateformes prises en charge :** Windows, consoles de la famille XBOX One et consoles XBOX Series

## Voir aussi

[IAudioStateMonitor](/fr-CA/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor)

[À propos de WASAPI](https://learn.microsoft.com/windows/desktop/CoreAudio/wasapi)


## Related topics

- [IAudioStateMonitor::RegisterCallback method](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback.md)
- [Método IAudioStateMonitor::RegisterCallback](/es/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback.md)
- [IAudioStateMonitor::RegisterCallback 方法](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback.md)
- [IAudioStateMonitor::RegisterCallback メソッド](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback.md)
- [IAudioStateMonitor::UnregisterCallback method](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback.md)
