编辑:我没有将相应的标志传递给initialize方法,我认为MSDN上的音频会话页面在这方面略有误导,因为它似乎暗示它是默认行为,但是默认行为是会话与进程关联,以允许它们在您需要传递的流发布时终止:
AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
http://msdn.microsoft.com/en-us/library/windows/desktop/dd370789(v=vs.85).aspx
好吧所以我一直试图让一些音频工作,基本上我想在播放音频和视频的同一过程中打开多个窗口(音频在除了最顶层之外的所有部分都是静音的)。我遇到audio sessions时遇到问题,我为每个流创建一个唯一的会话,但是当我认为我关闭了流时,会话没有关闭。
我的理解是sndvol将显示处于活动/非活动状态的会话,当您释放对流的所有引用时,会话将设置为已过期且不再出现在sndvol中,但是我发现对于每个流我打开一个新会话出现在sndvol中,但是在整个过程结束之前不会被删除,而是在音频线程结束时(即当我关闭窗口/更改输入时)。
这是我的音频线程,我已经将它剥离到它的骨头,而IAudioSessionControl
接口只是用来帮助我找出要调试的会话状态,在'音频循环'中它是活跃的,其他地方都处于非活动状态,但我认为它会在我释放pAudioClient
之后过期我在这里错了,我还应该发布什么呢?
WORD WINAPI
AudioThread (
LPVOID lpThreadParameter )
{
HRESULT hr;
PAUDIOPARMS pAudioParms = (PAUDIOPARMS)lpThreadParameter;
IMMDeviceEnumerator *pEnumerator = NULL;
IMMDevice *pDevice = NULL;
IAudioClient *pAudioClient = NULL;
IAudioSessionControl *pAudioSessionControl;
AudioSessionState audioSessionState;
GUID sessionGUID;
WAVEFORMATEXTENSIBLE *pwfx = NULL;
/* Initialise WASAPI interfaces and find audio endpoint. */
hr = CoCreateInstance ( &CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
&IID_IMMDeviceEnumerator, (void**)&pEnumerator);
EXIT_ON_ERROR(hr)
hr = pEnumerator->lpVtbl->GetDefaultAudioEndpoint(pEnumerator, eRender,
eConsole, &pDevice);
EXIT_ON_ERROR(hr)
SAFE_RELEASE ( pEnumerator );
hr = pDevice->lpVtbl->Activate(pDevice, &IID_IAudioClient, CLSCTX_ALL,
NULL, (void**)&pAudioClient);
EXIT_ON_ERROR(hr)
SAFE_RELEASE ( pDevice );
hr = pAudioClient->lpVtbl->GetMixFormat(pAudioClient,&pwfx);
CoCreateGuid ( &sessionGUID );
hr = pAudioClient->lpVtbl->Initialize ( pAudioClient, AUDCLNT_SHAREMODE_SHARED,
0, 853333, 0, pwfx, &sessionGUID );
if ( pwfx )
CoTaskMemFree ( pwfx );
hr = pAudioClient->lpVtbl->GetService ( pAudioClient, &IID_IAudioSessionControl, (void**)&pAudioSessionControl );
pAudioSessionControl->lpVtbl->GetState ( pAudioSessionControl, &audioSessionState );
hr = pAudioClient->lpVtbl->Start(pAudioClient);
EXIT_ON_ERROR(hr)
/* Allow OpenSharedAudio thread to continue. */
SetEvent ( pAudioParms->HStartEvent );
while (!pAudioParms->BClose)
{
/* Audio Loop. */
pAudioSessionControl->lpVtbl->GetState ( pAudioSessionControl, &audioSessionState );
}
hr = pAudioClient->lpVtbl->Stop(pAudioClient);
EXIT_ON_ERROR(hr)
Exit:
SAFE_RELEASE ( pAudioClient );
pAudioParms->Error = hr;
pAudioSessionControl->lpVtbl->GetState ( pAudioSessionControl, &audioSessionState );
SAFE_RELEASE ( pAudioSessionControl );
/* Allow OpenSharedAudio thread to continue. */
SetEvent ( pAudioParms->HStartEvent );
return 0;
}
我的SAFE_RELEASE()
定义如下:
#define SAFE_RELEASE(pUnk) \
if ((pUnk) != NULL) \
{ (pUnk)->lpVtbl->Release(pUnk); (pUnk) = NULL; }