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

> IAudioStateMonitor::GetSoundLevel メソッド

# IAudioStateMonitor::GetSoundLevel メソッド

\[一部の情報は、市販前に大幅に変更される可能性のあるプレリリース製品に関するものです。Microsoft は、ここに記載されている情報に関して、明示または黙示のいかなる保証も行いません。]

[IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) に関連付けられているストリームの現在のサウンド レベルを取得します。

## 構文

```cpp theme={null}
AudioStateMonitorSoundLevel GetSoundLevel();
```

### パラメーター

このメソッドにはパラメーターはありません。

### 戻り値

[IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) に関連付けられているストリームの現在のサウンド レベルを示す、[AudioStateMonitorSoundLevel](/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel) 列挙型の値です。

## 解説

次のシナリオを考えてみます。ゲームが、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. 
} 
```

[AudioStateMonitorSoundLevel](/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel) 列挙型の値です。[IAudioStateMonitor](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) に関連付けられているストリームの現在のサウンド レベルを示します。

## 要件

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

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

## 関連項目

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

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


## Related topics

- [AudioStateMonitorSoundLevel 列挙型](/ja-jp/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel.md)
- [IAudioStateMonitor::RegisterCallback メソッド](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-registercallback.md)
- [IAudioStateMonitor::UnregisterCallback メソッド](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-unregistercallback.md)
- [IAudioStateMonitor インターフェイス](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor.md)
- [AudioStateMonitor](/ja-jp/reference/audio/audiostatemonitor/audiostatemonitor_members.md)
