c# - 添加声音过滤器并连接声音针脚

标签 c# audio directshow avi directshow.net

语境

具有用户控件的WPF UI,该控件可实例化多个COMS并在directshow.net中使用过滤器

问题

音频引脚的名称根据正在播放的视频而变化。 (都是.avi文件)
如您在屏幕截图中看到的,声音引脚不相同。 (一个是“Stream 01”,另一个是“01 Microsoft wave form .....”)

在我的代码中,我使用ConnectDirect和GetPin方法。要使用GetPin,您需要提供一个引脚名称。

图表

用完全相同的代码生成图形,只更改视频文件。



当引脚名称根据运行的.avi文件而变化时,如何连接过滤器?顺便说一句,一个avi文件是“自制的”,而另一个是Microsoft avi示例文件(12秒蓝色时钟)

相关代码

//sound filter linker
IBaseFilter pACMWrapper = (IBaseFilter)new ACMWrapper();
hr = m_FilterGraph.AddFilter(pACMWrapper, "ACM wrapper");


//add le default direct sound device

IBaseFilter pDefaultDirectSoundDevice = null;

try
{
    pDefaultDirectSoundDevice = (IBaseFilter)new DSoundRender();
    hr = m_FilterGraph.AddFilter(pDefaultDirectSoundDevice, "Default DirectSound Device");


    IBaseFilter aviSplitter;
    //find the avi splitter automatically added when I connect samp grabber to source filter.
    m_FilterGraph.FindFilterByName("AVI Splitter", out aviSplitter);

    System.Windows.MessageBox.Show(""); // graph screenshot is from here.

    hr = m_FilterGraph.Connect(GetPin(aviSplitter, "Stream 01"), GetPin(pACMWrapper, "Input"));
    DsError.ThrowExceptionForHR(hr);

    //connect audio filters 
    hr = m_FilterGraph.ConnectDirect(GetPin(pACMWrapper, "Output"), GetPin(pDefaultDirectSoundDevice, "Audio Input pin (rendered)"), null);
    DsError.ThrowExceptionForHR(hr);
}
catch (Exception)
{
    pDefaultDirectSoundDevice = null;
    //log error, play video without sound
    //throw;
}

GetPin代码
    private IPin GetPin(IBaseFilter destinationFilter, string pinName)
    {
        IEnumPins pinEnum;
        int hr = destinationFilter.EnumPins(out pinEnum);
        DsError.ThrowExceptionForHR(hr);

        IPin[] pins = new IPin[1];

        IntPtr fetched = Marshal.AllocCoTaskMem(4);

        while (pinEnum.Next(1, pins, fetched) == 0)
        {
            PinInfo pInfo;
            pins[0].QueryPinInfo(out pInfo);

            bool found = (pInfo.name == pinName);
            DsUtils.FreePinInfo(pInfo);
            if (found)
                return pins[0];
        }
        return null;
    }

最佳答案

您无需使用硬编码名称选择输出引脚。相反,实际上这是一种更可靠的方法,您需要枚举引脚(就像GetPin函数已经做的那样),然后枚举给定引脚上的媒体类型。可以仅查看第一种媒体类型(如果有)。如果其主要类型是MEDIATYPE_Audio,则不管您的有效名称如何,它都是您的密码。

关于c# - 添加声音过滤器并连接声音针脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26369437/

相关文章:

c# - 当字段名包含空格时反序列化 JSON

c# - WPF MVVM ItemsControl 具有多个 ViewModel,具体取决于对象类型

delphi - 用于动态转换音频采样率的 DSPACK 示例?

c# - 是否可以将现有的 Windows Phone 8 应用程序更新到 Windows Phone Store 8.1

c# - LINQ - 如果我有很多更改,我应该以某种方式定期 SubmitChanges() 吗?

python - 如何在 python 中获取 GFCC 而不是 MFCC?

audio - FFMPEG AAC 编码器问题

Android:OpenCore 是否支持音频效果?

c++ - 如何使用 directshow 执行进程间(非线程)流共享?

ffmpeg总是通过directshow传递视频?