当然.与Windows API中的许多其他内容一样,播放.mp3文件的方法不止一种.以编程方式执行此操作的"最简单"方法是使用DirectShow.MSDN文档甚至在一个名为"如何播放文件"的页面上包含一个最小代码示例,以帮助您入门:
// Visual C++ example
#include <dshow.h>
#include <cstdio>
// For IID_IGraphBuilder, IID_IMediaControl, IID_IMediaEvent
#pragma comment(lib, "strmiids.lib")
// Obviously change this to point to a valid mp3 file.
const wchar_t* filePath = L"C:/example.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();
}
Run Code Online (Sandbox Code Playgroud)
请务必仔细阅读DirectShow文档,以了解在正确的DirectShow应用程序中应该发生的事情.
要将媒体数据"提供"到图表中,您需要实现一个IAsyncReader.幸运的是,Windows SDK包含一个实现被IAsyncReader调用的示例CAsyncReader.该示例将媒体文件读入内存缓冲区,然后用于CAsyncReader将数据流式传输到图形中.这可能就是你想要的.在我的机器上,样本位于文件夹中C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\multimedia\directshow\filters\async.
您可以使用 mciSendString 控制音频通道(以便使其播放任何内容,包括 MP3)http://msdn.microsoft.com/en-us/library/ms709492%28VS.85%29.aspx
下面是一个例子(虽然是C#语言,但是原理基本一样):
C 语言中也有同样的原理:
http://www.daniweb.com/software-development/c/code/268167