在C ++中使用Windows API播放音频流

时间:2014-12-06 12:54:41

标签: c++ winapi directshow ms-media-foundation

要使用(在本例中)DirectShow在Windows中播放.mp3文件,您只需要:

#include <dshow.h>
#include <cstdio>
// For IID_IGraphBuilder, IID_IMediaControl, IID_IMediaEvent
#pragma comment(lib, "strmiids.lib") 

const wchar_t* filePath = L"C:/Users/Public/Music/Sample Music/Sleep Away.mp3";

int main()
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;

    // Initialize the COM library.
    HRESULT hr = ::CoInitialize(NULL);
    if (FAILED(hr))
    {
        ::printf("ERROR - Could not initialize COM library");
        return 0;
    }

    // Create the filter graph manager and query for interfaces.
    hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
        IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        ::printf("ERROR - Could not create the Filter Graph Manager.");
        return 0;
    }

    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    // Build the graph.
    hr = pGraph->RenderFile(filePath, NULL);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    // Clean up in reverse order.
    pEvent->Release();
    pControl->Release();
    pGraph->Release();
    ::CoUninitialize();
}

我无法找到类似这样的方法,但能够改为播放.asx,例如:http://listen.radiotunes.com/public5/solopiano.asx

在MSDN中,我只能找到在C#中创建Forms应用程序并在表单中插入WindowsMediaPlayer控件的方法。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

.asx文件实际上是一个播放列表。请参阅here有关格式的一些信息。

DirectShow不支持

.asx。有关支持的格式,请参阅here

您可以解析该文件,因为它是XML,并找到该流的实际URL,然后播放它,或者您可以使用Windows Media Player SDK。您可以看到WM SDK here的一些示例代码。

答案 1 :(得分:0)

好的,让它使用从here获取的此示例并添加此额外行:hr = spPlayer->put_URL(L"http://listen.radiotunes.com/public5/solopiano.asx");

#include "atlbase.h"
#include "atlwin.h"
#include "wmp.h"
#include <cstdio>

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);

    HRESULT hr = S_OK;
    CComBSTR bstrVersionInfo; // Contains the version string.
    CComPtr<IWMPPlayer> spPlayer;  // Smart pointer to IWMPPlayer interface.



    hr = spPlayer.CoCreateInstance(__uuidof(WindowsMediaPlayer), 0, CLSCTX_INPROC_SERVER);

    if (SUCCEEDED(hr))
    {
        hr = spPlayer->get_versionInfo(&bstrVersionInfo);
        hr = spPlayer->put_URL(L"http://listen.radiotunes.com/public5/solopiano.asx");

    }

    if (SUCCEEDED(hr))
    {
        // Show the version in a message box.
        COLE2T pStr(bstrVersionInfo);
        MessageBox(NULL, (LPCSTR)pStr, _T("Windows Media Player Version"), MB_OK);
    }

    // Clean up.
    spPlayer.Release();
    CoUninitialize();

    return 0;
}