> ## 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](/zh-TW/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) 相關聯之串流的目前音量層級。

## 語法

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

### 參數

此方法沒有參數。

### 傳回值

[AudioStateMonitorSoundLevel](/zh-TW/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel) 列舉中的值，指出與 [IAudioStateMonitor](/zh-TW/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) 相關聯之串流的目前音量層級。

## 備註

請考慮下列案例：遊戲只有在確知音訊不會因原則而被 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](/zh-TW/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel) 列舉中的值。指出與 [IAudioStateMonitor](/zh-TW/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor) 相關聯之串流的目前音量層級。

## 需求

**標頭：** Audiostatemonitorapi.h

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 另請參閱

[IAudioStateMonitor](/zh-TW/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor)

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


## Related topics

- [IAudioStateMonitor::GetSoundLevel 方法](/zh-CN/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel.md)
- [AudioStateMonitorSoundLevel 枚举](/zh-CN/reference/audio/audiostatemonitor/enums/audiostatemonitorsoundlevel.md)
- [IAudioStateMonitor::GetSoundLevel method](/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel.md)
- [Método IAudioStateMonitor::GetSoundLevel](/es/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel.md)
- [IAudioStateMonitor::GetSoundLevel メソッド](/ja-jp/reference/audio/audiostatemonitor/interfaces/iaudiostatemonitor-getsoundlevel.md)
