音频录制(windows)

 

void start_record_audio() {  APP_LOG("<<<<<<start === record audio xp >>>>>>>>>>>>>>>>>>>");

 pGraph_xp = NULL;  HRESULT h_com_ret = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,   IID_IGraphBuilder, (void **)&pGraph_xp);

 if(pGraph_xp == NULL)  {   APP_LOG("pGraph_xp is NULL ....");   return ;  }

 ICaptureGraphBuilder2 * pBuilder = NULL;  h_com_ret = CoCreateInstance(CLSID_CaptureGraphBuilder2,0,CLSCTX_INPROC_SERVER,IID_ICaptureGraphBuilder2,(void**)&pBuilder);  if(pBuilder)   pBuilder->SetFiltergraph(pGraph_xp);  else   {   APP_LOG("pBuilder is NULL ....%d",h_com_ret);   return ;  }

 pMediaControl_xp = NULL;  pGraph_xp->QueryInterface(IID_IMediaControl,(void**)&pMediaControl_xp);  if(NULL == pMediaControl_xp)  {   APP_LOG("pMediaControl_xp is NULL ....");   return ;  }

 ICreateDevEnum *pDevEnum = NULL;  h_com_ret = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,   IID_ICreateDevEnum, (void **)&pDevEnum);

 IEnumMoniker *pClassEnum = NULL;  pDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pClassEnum, 0);  IMoniker *pMoniker = NULL;  ULONG cFetched;  IBaseFilter *pSrc = NULL;  if (pClassEnum && (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK))  {   if(pMoniker)   {    pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);    pMoniker->Release();   }   else   {    APP_LOG("pClassEnum is NULL ....");   }  }  else  {   APP_LOG("pClassEnum is NULL ....");  }

 if(pClassEnum)   pClassEnum->Release();

 IBaseFilter *pWaveDest = NULL ,*pWriter = NULL;  h_com_ret = CoCreateInstance(CLSID_WavDest, NULL, CLSCTX_ALL,   IID_IBaseFilter, (void **)&pWaveDest);  if(SUCCEEDED(h_com_ret))  {}  else  {   APP_LOG("CoCreateInstance failed ...... %u",h_com_ret);  }

 h_com_ret = CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_ALL,   IID_IBaseFilter, (void **)&pWriter);  if(SUCCEEDED(h_com_ret))  {}  else  {   APP_LOG("CoCreateInstance failed ...... %d",h_com_ret);  }

 if(pSrc)   pGraph_xp->AddFilter(pSrc,L"Wav");  else  {   APP_LOG("pSrc is NULL ....");  }

 if(pWaveDest)   pGraph_xp->AddFilter(pWaveDest,L"WavDest");  else  {   APP_LOG("pWaveDest is NULL ....");  }

 if(pWriter)   pGraph_xp->AddFilter(pWriter,L"FileWriter");  else  {   APP_LOG("pWriter is NULL ....");  }

 IFileSinkFilter2* pSink = NULL;  pWriter->QueryInterface(IID_IFileSinkFilter2,(void**)&pSink);

 if(pSink)  {   CString audio_fname = convert_string_ex(m_cap_fname_ex);   pSink->SetFileName(audio_fname,NULL);   pSink->SetMode(AM_FILE_OVERWRITE);  }  else  {   APP_LOG("pSink is NULL ....");  }

 AM_MEDIA_TYPE type;  type.majortype = MEDIATYPE_Stream;  type.subtype =MEDIASUBTYPE_WAVE;  type.formattype = FORMAT_None;  type.bFixedSizeSamples = FALSE;  type.bTemporalCompression = FALSE;  type.pUnk = NULL;

 // source filter, output pin  IPin* pOutpin = FindPin(pSrc,PINDIR_OUTPUT);

 IPin* pInpin = NULL,*pOut = NULL;

 // transform filter, output pin  pOut = FindPin_xp(pWaveDest,PINDIR_OUTPUT);

 // transform filter, input pin  pInpin = FindPin_xp(pWaveDest,PINDIR_INPUT);

 // render filter, input pin  IPin* pInpin1= FindPin_xp(pWriter,PINDIR_INPUT); 

 if(pOutpin && pInpin)   pGraph_xp->ConnectDirect(pOutpin,pInpin,NULL);  else  {   APP_LOG("pOutpin pInpin is NULL ....");  }

 if(pOut && pInpin1)   pGraph_xp->ConnectDirect(pOut,pInpin1,NULL);  else  {   APP_LOG("pOut pInpin1 is NULL ....");  }

 {   APP_LOG("START RECORD AV FILE ......");  }  pMediaControl_xp->Run(); }

IPin* FindPin(IBaseFilter *pFilter, PIN_DIRECTION dir) {  if(NULL == pFilter)   return NULL;

 IEnumPins* pEnumPins;  IPin* pOutpin = NULL;  PIN_DIRECTION pDir;  pFilter->EnumPins(&pEnumPins);  if(NULL == pEnumPins)   return NULL;

 while (pEnumPins->Next(1,&pOutpin,NULL)==S_OK)  {   if(pOutpin)    pOutpin->QueryDirection(&pDir);

  if (pDir == dir)   {    return pOutpin;   }  }  return NULL; }

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值