> ## 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::RegisterCallback メソッド

> IAudioStateMonitor::RegisterCallback メソッド

# IAudioStateMonitor::RegisterCallback メソッド

[IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) に関連付けられているストリームのオーディオ レベルが変化したときにシステムから呼び出されるコールバック関数を登録します。

## 構文

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

### パラメーター

*callback*\
型: AudioStateMonitorCallback\*

[IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) に関連付けられているストリームのオーディオ レベルに変化があったときに、システムによって呼び出されるコールバック関数へのポインターです。

*context*\
型: void\*

*callback* パラメーターで指定した関数に渡される、アプリ定義のコンテキスト データへのポインターです。

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

コールバック登録を識別する 64 ビット整数です。*callback* パラメーターで指定した関数の登録を解除するには、この値を [IAudioStateMonitor::UnregisterCallback](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback) に渡します。

### 戻り値

型: HRESULT

メソッドが成功した場合、S\_OK が返されます。失敗した場合、返されるコードには次の表に示す値が含まれますが、これらに限定されません。

| 戻りコード                                   | 説明                                |
| --------------------------------------- | --------------------------------- |
| <dl> <dt> **E\_OUTOFMEMORY**</dt> </dl> | 指定されたコールバック ポインターが null です。<br /> |

## 解説

次のシナリオを考えてみます。ゲームが、OS のポリシーによってオーディオがミュートされないことがわかっている場合にのみ 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. 
} 
```

## 要件

**ヘッダー:** Audiostatemonitorapi.h

**サポートされるプラットフォーム:** Windows、XBOX One ファミリ本体および XBOX Series 本体

## 関連項目

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

[WASAPI について](https://learn.microsoft.com/windows/desktop/CoreAudio/wasapi)


## Related topics

- [IAudioStateMonitor::UnregisterCallback メソッド](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback.md)
- [IAudioStateMonitor::GetSoundLevel メソッド](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel.md)
- [IAudioStateMonitor インターフェイス](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor.md)
- [AudioStateMonitorRegistrationHandle](/ja-jp/reference/audio/audiostatemonitor/types/audiostatemonitorregistrationhandle.md)
- [AudioStateMonitor](/ja-jp/reference/audio/audiostatemonitor/audiostatemonitor_members.md)
