> ## 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 インターフェイス

> IAudioStateMonitor インターフェイス

# IAudioStateMonitor インターフェイス

デスクトップ アプリやゲームが、自身のオーディオ ストリームのサウンド レベルがシステムによって変更されるタイミングを検出するためのメソッドを提供します。次のようなシナリオが含まれます。

* ゲーム メディア ストリームが、バックグラウンドで再生されているオーディオ アプリによってミュートされたタイミングを知りたい場合
* バックグラウンドで VOIP 通話が始まったために、ゲーム チャットのオーディオ ストリームを停止すべきタイミングを知りたい場合

デスクトップ アプリやゲームが、自身のオーディオ ストリームのサウンド レベルがシステムによって変更された時刻を把握するためのメソッドを提供します。ゲームまたはアプリが以下を判断する必要があるシナリオが含まれます。

* バックグラウンドで再生されているオーディオ アプリによってゲーム メディア ストリームがミュートされた時刻
* バックグラウンドで VoIP 通話が開始されたためにゲーム チャットのオーディオ ストリームを停止する時刻

サウンド レベルは、レンダー ストリームだけでなくキャプチャ ストリームについても確認および監視できます。監視対象のストリームは、カテゴリ、オーディオ エンドポイント、またはデバイス ロールによって選択できます。新しいストリームを作成する前に、そのストリームが取り得るサウンド レベルを事前に確認することもできます。

この API は、UWP アプリで使用可能な [Windows.Media.Audio.AudiostateMonitor クラス](https://learn.microsoft.com/uwp/api/windows.media.audio.audiostatemonitor) と同等のものですが、デスクトップ アプリやゲーム向けのものです。このインターフェイスは UWP アプリでは使用できません。

## メンバー

**IAudioStateMonitor** インターフェイスは [IUnknown](https://learn.microsoft.com/windows/desktop/api/unknwn/nn-unknwn-iunknown) インターフェイスを継承していますが、次のような種類のメンバーも持っています。

### メソッド

**IAudioStateMonitor** インターフェイスには次のメソッドがあります。

| メソッド                                                                                                      | 説明                                                                                                                                                |
| --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| [GetSoundLevel](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel)           | **IAudioStateMonitor** に関連付けられているストリームの現在のサウンド レベルを取得します                                                                                          |
| [RegisterCallback](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback)     | **IAudioStateMonitor** に関連付けられているストリームのオーディオ レベルに変化があったときに、システムから呼び出されるコールバック関数を登録します                                                             |
| [UnregisterCallback](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback) | [IAudioStateMonitor::RegisterCallback](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback) で以前に登録したコールバックの登録を解除します |

## 解説

このインターフェイスのインスタンスを取得するには、以下のいずれかのメソッドを呼び出します。

* [CreateCaptureAudioStateMonitor](/reference/audio/audiostatemonitor/functions/createcaptureaudiostatemonitor)
* [CreateCaptureAudioStateMonitorForCategory](/reference/audio/audiostatemonitor/functions/createcaptureaudiostatemonitorforcategory)
* [CreateCaptureAudioStateMonitorForCategoryAndDeviceId](/reference/audio/audiostatemonitor/functions/createcaptureaudiostatemonitorforcategoryanddeviceid)
* [CreateCaptureAudioStateMonitorForCategoryAndDeviceRole](/reference/audio/audiostatemonitor/functions/createcaptureaudiostatemonitorforcategoryanddevicerole)
* [CreateRenderAudioStateMonitor](/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitor)
* [CreateRenderAudioStateMonitorForCategory](/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitorforcategory)
* [CreateRenderAudioStateMonitorForCategoryAndDeviceId](/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitorforcategoryanddeviceid)
* [CreateRenderAudioStateMonitorForCategoryAndDeviceRole](/reference/audio/audiostatemonitor/functions/createrenderaudiostatemonitorforcategoryanddevicerole)

次のコード例では、ゲームが [AudioCategory\_GameMedia](https://learn.microsoft.com/windows/desktop/api/audiosessiontypes/ne-audiosessiontypes-audio_stream_category) カテゴリのオーディオ レンダー ストリームに対してオーディオ レベル通知を登録するシナリオを示します。

このカテゴリのサウンド レベルがミュートされている場合、ゲームは自身のゲーム メディア ストリームの再生を停止します。サウンド レベルがそれ以外の値に設定されている場合は、ゲーム メディア ストリームが再生されます。

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

// Returns true if the GameMedia stream is to be disabled, false if it's to 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 whether the GameMedia stream is to be enabled.
        bool disableGameMediaStream = IsSoundLevelMuted(g_audioStateMonitor.get());

        // Here, add code that enables or disables the GameMedia stream 
        // according to 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, &amp;g_registration);
        }
    }

    if (FAILED(hr))
    {
        // g_audioStateMonitor is a smart pointer, so if an IAudioStateMonitor 
        // was allocated, then setting the smart pointer to null invokes the
        // Release method, which will decrement its usage count and ensure that 
        // the object is destroyed properly.
        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
        // will invoke 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 
    // according to the value of the Boolean.
}
```

## 要件

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

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

## 関連項目

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


## Related topics

- [AudioStateMonitor](/ja-jp/reference/audio/audiostatemonitor/audiostatemonitor_members.md)
- [IXtfRemoteRunCallback インターフェイス](/ja-jp/reference/tools/xtf/xtfremoterun/classes/IXtfRemoteRunCallback/interfaces/ixtfremoteruncallback.md)
- [IXtfRemoteRunClient インターフェイス](/ja-jp/reference/tools/xtf/xtfremoterun/classes/IXtfRemoteRunClient/interfaces/ixtfremoterunclient.md)
- [さまざまな GameInput デバイス種別とのインターフェイス](/ja-jp/build/core-features/common/input/hardware/input-hardware-interfaces.md)
- [GameInput フォースフィードバックインターフェイス](/ja-jp/build/core-features/common/input/hardware/input-hardware-force-feedback.md)
