Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c++ - IAudioSessionNotification, anyone have working code?

I'm picking up some experimental code I was messing with in the Windows 7 Beta now that I've installed the RC.

Basically, I'm trying to get IAudioSessionManager2 & IAudioSessionNotification working together to inform my little app of every new audio session created.

Punchline code in AudioListener (public IAudioSessionNotification):

//This is mostly lifted from MSDN
HRESULT STDMETHODCALLTYPE AudioListener::QueryInterface(REFIID riid, void** ppvObject)
{
    if(riid == __uuidof(IUnknown))
    {
        *ppvObject = (IUnknown*)this;
        return S_OK;
    }

    if(riid == __uuidof(IAudioSessionNotification))
    {
        *ppvObject = (IAudioSessionNotification*)this;
        return S_OK;
    }

    *ppvObject = NULL;

    return E_NOINTERFACE;
}

//m_hwnd, and WM_SESSION_CREATED are set to good values
//WM_SESSION_CREATEd via RegisterWindowMessage(...)
HRESULT STDMETHODCALLTYPE AudioListener::OnSessionCreated(IAudioSessionControl *pSession)
{
    PostMessage(m_hwnd, WM_SESSION_CREATED, (WPARAM)pSession, 0);

    return S_OK;
}

Code registering my listener:

BOOL RegisterMonitor(HWND target)
{
    BOOL success = false;

    HRESULT res;
    IMMDevice* pDevice;
    IMMDeviceEnumerator* pEnumerator;

    SESSION_LISTENER = NULL;
    SESSION = NULL;

    res = CoInitialize(NULL);

    if(res != S_OK && res != S_FALSE)
        return false;

    SESSION_LISTENER = new AudioListener(target);

    res = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);
    if(res != S_OK)  goto Exit;

    res = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
    if(res != S_OK)  goto Exit;

    res = pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&SESSION);
    if(res != S_OK)  goto Exit;

    res = SESSION->RegisterSessionNotification(SESSION_LISTENER);
    if(res != S_OK)  goto Exit;

    success = true;

Exit:
    SAFE_RELEASE(pEnumerator);
    SAFE_RELEASE(pDevice);
    if(!success)
    {
        SAFE_RELEASE(SESSION_LISTENER);
        SAFE_RELEASE(SESSION);
    }

    return success;
}

RegisterMonitor(...) returns true, but no notifications are ever received. I've been testing by launching little apps with minor sound effects and triggering them (Soltaire, Minesweeper, etc.), confirming that they show up in SndVol when I'm expecting to see a notification.

Basically, does anyone see what I'm doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You released the session manager in your RegisterMonitor function. Once you release the last reference to the session manager it is freed and you'll no longer receive session notifications.

Keep the session manager object alive and it should work just fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...