HRESULT CAudioSessionVolume::Initialize() { HRESULT hr = S_OK; IMMDeviceEnumerator *pDeviceEnumerator = NULL; IMMDevice *pDevice = NULL; IAudioSessionManager *pAudioSessionManager = NULL; // Get the enumerator for the audio endpoint devices. hr = CoCreateInstance( __uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDeviceEnumerator) ); if (FAILED(hr)) { goto done; } // Get the default audio endpoint that the SAR will use. hr = pDeviceEnumerator->GetDefaultAudioEndpoint( eRender, eConsole, // The SAR uses 'eConsole' by default. &pDevice ); if (FAILED(hr)) { goto done; } // Get the session manager for this device. hr = pDevice->Activate( __uuidof(IAudioSessionManager), CLSCTX_INPROC_SERVER, NULL, (void**) &pAudioSessionManager ); if (FAILED(hr)) { goto done; } // Get the audio session. hr = pAudioSessionManager->GetAudioSessionControl( &GUID_NULL, // Get the default audio session. FALSE, // The session is not cross-process. &m_pAudioSession ); if (FAILED(hr)) { goto done; } hr = pAudioSessionManager->GetSimpleAudioVolume( &GUID_NULL, 0, &m_pSimpleAudioVolume ); done: SafeRelease(&pDeviceEnumerator); SafeRelease(&pDevice); SafeRelease(&pAudioSessionManager); return hr; }
//------------------------------------------------------------------- // Initializes the CAudioSessionEventHandler object. long CAudioSessionEventHandler::Initialize() { _WINQ_FCONTEXT( "CAudioSessionEventHandler::Initialize" ); long hr = 0; IMMDeviceEnumerator* pDeviceEnumerator = 0; IMMDevice* pDevice = 0; IAudioSessionManager* pAudioSessionManager = 0; // Get the enumerator for the audio endpoint devices. hr = nsWinQAPI::COLE32::Instance().CoCreateInstance( reinterpret_cast< const ::IID& >( CLASS_MMDEVICEENUMERATOR ), 0, CLSCTX_INPROC_SERVER, reinterpret_cast< const ::IID& >( IMMDeviceEnumerator::_IID ), reinterpret_cast< void** >( &pDeviceEnumerator ) ); if( hr < 0 ) { goto done; } // Get the default audio endpoint that the SAR will use. hr = pDeviceEnumerator->GetDefaultAudioEndpoint( eRender, eConsole, // The SAR uses 'eConsole' by default. &pDevice ); if( hr < 0 ) { goto done; } // Get the session manager for this device. hr = pDevice->Activate( IAudioSessionManager::_IID, CLSCTX_INPROC_SERVER, 0, (void**) &pAudioSessionManager ); if( hr < 0 ) { goto done; } // Get the audio session. hr = pAudioSessionManager->GetAudioSessionControl(reinterpret_cast< const nsWin32::GUID* >(&NULL_GUID), // Get the default audio session. 0, // The session is not cross-process. &m_pAudioSession ); if( hr < 0 ) { goto done; } hr = pAudioSessionManager->GetSimpleAudioVolume(reinterpret_cast< const nsWin32::GUID* >(&NULL_GUID), 0, &m_pSimpleAudioVolume); done: pDeviceEnumerator->Release(); pDevice->Release(); pAudioSessionManager->Release(); return hr; }
//设置应用程序静音 bool CCoreAudioVolume::SetMute(BOOL bMute) { //IAudioSessionControl *pAudioSection = NULL; ISimpleAudioVolume *pAudioVolume = NULL; IAudioSessionManager *pManager = NULL; IMMDevice *pDevice = NULL; HRESULT hr; std::vector<IMMDevice*>::iterator iter = m_vArrayDevice.begin(); for(; iter != m_vArrayDevice.end(); iter++) { pDevice = *iter; ATLASSERT(pDevice != NULL); hr = pDevice->Activate(__uuidof(IAudioSessionManager), CLSCTX_INPROC_SERVER, NULL, (void**)&pManager); if(FAILED(hr)) continue; hr = pManager->GetAudioSessionControl(NULL, 0, &m_pAudioSection); CMutedSessionEvents* pMutedse=new CMutedSessionEvents(pManager); hr = m_pAudioSection->RegisterAudioSessionNotification(pMutedse); hr = pManager->GetSimpleAudioVolume(NULL, 0, &pAudioVolume); if(SUCCEEDED(hr)) { pAudioVolume->SetMute(bMute, &GUID_NULL); //pAudioVolume->SetMasterVolume() } else { ATLASSERT(FALSE); } } SAFE_RELEASE(pManager); SAFE_RELEASE(pAudioVolume); return true; }