IAudioSessionControl2 介面 (audiopolicy.h)

用戶端可以使用 IAudioSessionControl2 介面來取得音訊會話的相關信息。

若要取得 IAudioSessionControl2 介面的參考,應用程式必須呼叫 IAudioSessionControl::QueryInterface ,才能從數據流物件的 IAudioSessionControl 介面要求介面指標。 應用程式有兩種方式可以取得 IAudioSessionControl 介面的指標:

當應用程式想要釋放 IAudioSessionControl2 介面實例時,應用程式必須從與呼叫建立物件的 IAudioClient::GetService 相同的線程呼叫介面的 Release 方法。

使用這個介面的應用程式線程必須針對 COM 初始化。 如需 COM 初始化的詳細資訊,請參閱 Windows SDK 檔中 的 CoInitializeEx 函式描述。

繼承

IAudioSessionControl2 介面繼承自 IAudioSessionControlIAudioSessionControl2 也有下列類型的成員:

方法

IAudioSessionControl2 介面具有這些方法。

 
IAudioSessionControl2::GetProcessId

GetProcessId 方法會擷取音訊會話的進程標識碼。
IAudioSessionControl2::GetSessionIdentifier

GetSessionIdentifier 方法會擷取音訊會話標識碼。
IAudioSessionControl2::GetSessionInstanceIdentifier

GetSessionInstanceIdentifier 方法會擷取音訊會話實例的標識符。
IAudioSessionControl2::IsSystemSoundsSession

IsSystemSoundsSession 方法會指出會話是否為系統音效會話。
IAudioSessionControl2::SetDuckingPreference

SetDuckingPreference 方法可啟用或停用系統提供的預設數據流衰減體驗 (自動降低) 。

備註

此介面支援 串流衰減擷取的自定義實作,這是 Windows 7 的新功能。 播放媒體數據流的應用程式在預設通訊裝置上開啟新的通訊數據流時,可能會讓它的行為不同。 例如,當新的通訊數據流開啟時,可以暫停原始媒體數據流。 如需這項功能的詳細資訊,請參閱 預設的擷取體驗

應用程式可以使用此介面來執行下列工作:

  • 指定它想要退出系統所提供的預設數據流衰減體驗。
  • 取得與數據流相關聯的音訊會話標識碼。 通知註冊期間需要標識碼。 應用程式可以自行註冊,以接收來自系統的減少通知。
  • 檢查與音訊會話相關聯的數據流是否為系統音效。

範例

下列範例程式代碼示範如何取得 IAudioSessionControl2 介面的參考,並呼叫其方法來判斷與音訊會話相關聯的數據流是否為系統音效。

HRESULT SetDuckingForSystemSounds()
{
 
    HRESULT hr = S_OK;
    
    IMMDevice* pDevice = NULL;
    IMMDeviceEnumerator* pEnumerator = NULL;
    IAudioSessionControl* pSessionControl = NULL;
    IAudioSessionControl2* pSessionControl2 = NULL;
    IAudioSessionManager* pSessionManager = NULL;

    CHECK_HR( hr = CoInitialize(NULL));

    // Create the device enumerator.
    CHECK_HR( hr = CoCreateInstance(
        __uuidof(MMDeviceEnumerator), 
        NULL, CLSCTX_ALL, 
        __uuidof(IMMDeviceEnumerator), 
        (void**)&pEnumerator));

    // Get the default audio device.
    CHECK_HR( hr = pEnumerator->GetDefaultAudioEndpoint(
                    eRender, eConsole, &pDevice));

    // Get the audio client.
    CHECK_HR( hr = pDevice->Activate(
        __uuidof(IID_IAudioSessionManager), CLSCTX_ALL,
        NULL, (void**)&pSessionManager));

    // Get a reference to the session manager.
    CHECK_HR( hr = pSessionManager->GetAudioSessionControl (GUID_NULL, FALSE, &pSessionControl));
    
    // Get the extended session control interface pointer.
    CHECK_HR( hr = pSessionControl->QueryInterface(
        __uuidof(IAudioSessionControl2), (void**) &pSessionControl2));

    // Check whether this is a system sound.
    CHECK_HR( hr = pSessionControl2->IsSystemSoundsSession());

    // If it is a system sound, opt out of the default
    // stream attenuation experience.
    CHECK_HR( hr = pSessionControl2->SetDuckingPreference(TRUE));

done:

    // Clean up.
    SAFE_RELEASE(pSessionControl2);
    SAFE_RELEASE(pSessionControl);
    SAFE_RELEASE(pEnumerator);
    SAFE_RELEASE(pDevice);


    return hr;
}

規格需求

需求
最低支援的用戶端 Windows 7 [僅限傳統型應用程式]
最低支援的伺服器 Windows Server 2008 R2 [僅限傳統型應用程式]
目標平台 Windows
標頭 audiopolicy.h

另請參閱

核心音訊介面

IAudioSessionControl

使用通訊裝置