2014-12-06 84 views
0

要使用(在這種情況下)DirectShow的在Windows中發揮.mp3文件,你只需要:播放音頻流通過在C的Windows API ++

#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#中製作表格應用程序,將在表單中的WindowsMediaPlayer控制做到這一點。

任何想法?

回答

1

一個.asx文件實際上是一個播放列表。有關格式的一些信息,請參閱here

.asx不受DirectShow支持。有關支持的格式,請參見here

您可能會解析文件,因爲它是XML,並查找流的實際URL,然後播放它,或者可以使用Windows Media Player SDK。你可以看到WM SDK here的一些示例代碼。

+0

嗯,好吧,這就是了。我試圖[這](http://msdn.microsoft.com/en-us/library/windows/desktop/dd564579(v = vs.85).aspx)---但我如何使用該控制在C++中的示例[這裏](http://msdn.microsoft.com/en-us/library/windows/desktop/dd564119(v = vs.85).aspx)(也就是我認爲的JScript)... – Alex 2014-12-06 14:33:32

+0

WM sdk也應包含一個C++ api。你應該檢查文檔和標題。 – 2014-12-06 19:52:25

+0

是的,'IWMPPlayer'可以很好地完成這項工作。謝謝:) – Alex 2014-12-06 20:44:23

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; 
} 
+0

你不應該在答案中提出另一個問題。將它作爲另一個問題發佈。 – 2014-12-06 19:22:50

+0

@JonathanPotter行,刪除。 – Alex 2014-12-06 20:41:02