Windows DirectShow:如何播放多个MP3?

Windows DirectShow:如何播放多个MP3?,windows,mp3,directshow,latency,Windows,Mp3,Directshow,Latency,我必须在我的UI中播放多种声音,如按钮点击、错误、通知等。为此,我编写了以下代码(以前从未在Windows中播放过MP3): 看起来很慢。当我每秒点击一个按钮~5次时,UI就会冻结。我想,我不应该每次都调用Stop/Render/Run,但我不知道如何避免它。我应该如何重写代码 请注意,我没有使用DirectShow,而是尝试了libzplay(http://libzplay.sourceforge.net/) 让我引用它的自述: 如何将libZPlay与C结合使用++ 在源代码中包含libzp

我必须在我的UI中播放多种声音,如按钮点击、错误、通知等。为此,我编写了以下代码(以前从未在Windows中播放过MP3):

看起来很慢。当我每秒点击一个按钮~5次时,UI就会冻结。我想,我不应该每次都调用Stop/Render/Run,但我不知道如何避免它。我应该如何重写代码


请注意,

我没有使用DirectShow,而是尝试了libzplay(http://libzplay.sourceforge.net/)

让我引用它的自述:

如何将libZPlay与C结合使用++

  • 在源代码中包含libzplay.h
  • 包含libZPlay名称空间

    使用名称空间libZPlay

  • 将项目链接到导入库

    libzplay.lib-libzplay.dll的VC++导入库
    LbZPrasyBurL.LIB Borland C++ C++库的LBZPLAY libzplay.a-libzplay.dll的MinGW/GCC导入库

  • 创建ZPlay类

    ZPlay*player=CreateZPlay()

  • 打开文件

    如果(播放器->打开文件(“mySong.mp3”,sfAutodetect)=0){ //错误}

  • 开始玩

    player->Play()

  • 在游戏课结束时

    player->Release()

  • 它工作顺利。我可以只打开一次文件,每次需要时调用Play()。在DirectShow中调用Run()的方式不同


    这就是正确的库必须具备的条件:30分钟内从头开始嵌入,并且是坚实的岩石。

    这里的问题是,您重用相同的成员变量,或者在每次创建新变量时销毁以前创建的管道,或者甚至以更混乱的方式将它们混合在一起。不清楚您是否正确释放了接口指针。我在应用程序上调用Release()。
    bool ClientHandler::InitSoundAndMusic()
    {
        m_pSoundGraph = NULL;
        m_pSoundControl = NULL;
    
        HRESULT hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder,
                                        (void**) &m_pSoundGraph);
    
        if (!SUCCEEDED(hr) || !m_pSoundGraph)
            return false;
    
        hr = m_pSoundGraph->QueryInterface(IID_IMediaControl, (void**) &m_pSoundControl);
        if (!SUCCEEDED(hr) || !m_pSoundControl)
            return false;
    
        return true;
    }
    
    void ClientHandler::PlayFile(LPCTSTR file)
    {
        if (!m_pSoundGraph)
            return;
    
        m_pSoundControl->Stop();
        HRESULT hr = m_pSoundGraph->RenderFile(file, NULL);
        if (SUCCEEDED(hr))
            m_pSoundControl->Run();
    }
    
    bool ClientHandler::OnEvent(const String& message)
    {
        if (message == _T("button"))
        {
            PlayFile(_T(".\\sound\\button.mp3"));
            return true;
        }
    
        return false;
    }