BufferCB not being called by SampleGrabber

我是研究僧i 提交于 2019-12-25 02:21:34

问题


I'm using a SampleGrabber to get audio data, however my BufferCB method is not being executed. What am I doing wrong ?

//add Sample Grabber
            IBaseFilter pSampleGrabber = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_SampleGrabber));
            hr = pGraph.AddFilter(pSampleGrabber, "SampleGrabber");
            checkHR(hr, "Can't add Sample Grabber");

        AMMediaType pSampleGrabber_pmt = new AMMediaType();
        //pSampleGrabber_pmt.majorType = MediaType.Audio;
        pSampleGrabber_pmt.subType = MediaSubType.PCM;
        pSampleGrabber_pmt.formatType = FormatType.WaveEx;
        pSampleGrabber_pmt.fixedSizeSamples = true;
        pSampleGrabber_pmt.formatSize = 18;
        pSampleGrabber_pmt.sampleSize = 2;

        WaveFormatEx pSampleGrabber_Format = new WaveFormatEx();
        pSampleGrabber_Format.wFormatTag = 1;
        pSampleGrabber_Format.nChannels = 1;
        pSampleGrabber_Format.nSamplesPerSec = 48000;
        pSampleGrabber_Format.nAvgBytesPerSec = 96000;
        pSampleGrabber_Format.nBlockAlign = 2;
        pSampleGrabber_Format.wBitsPerSample = 16;
        pSampleGrabber_pmt.formatPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(pSampleGrabber_Format));
        Marshal.StructureToPtr(pSampleGrabber_Format, pSampleGrabber_pmt.formatPtr, false);
        hr = ((ISampleGrabber)pSampleGrabber).SetMediaType(pSampleGrabber_pmt);
        DsUtils.FreeAMMediaType(pSampleGrabber_pmt);
        checkHR(hr, "Can't set media type to sample grabber");

        ISampleGrabber pGrabber = new SampleGrabber() as ISampleGrabber; 
        pGrabber = (ISampleGrabber)pSampleGrabber;
        pGrabber.SetCallback(null, 1);

My BufferCB method is like

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
        {

            return 0;
        }

回答1:


You created and configured one instance pSampleGrabber and then you are attaching your callback to another unused idling instance pGrabber.

You need

pSampleGrabber as ISampleGrabber

instead of

new SampleGrabber() as ISampleGrabber


来源:https://stackoverflow.com/questions/10829575/buffercb-not-being-called-by-samplegrabber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!