繁体   English   中英

使用Windows内置MP3解码器播放音频?

[英]Use Windows built in MP3 decoder to play audio?

从Windows或Windows C + C ++开始,我如何使用Windows内置的MP3解码器?

我想播放一个mp3文件,而不必依赖任何其他第三方库,例如LAME.DLL。

我更新了问题以更好地适应我得到的答案,因为我非常喜欢它们。 相关问题。

当然。 与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();
}

请务必仔细阅读DirectShow文档,以了解在正确的DirectShow应用程序中应该发生的事情。


要将媒体数据“提供”到图形中,您需要实现IAsyncReader 幸运的是, Windows SDK包含一个实现名为CAsyncReaderIAsyncReader 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#中,但它基本上是相同的原理):

http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/152f0149-a62a-446d-a205-91256da7845d

这与C中的原理相同:

http://www.daniweb.com/software-development/c/code/268167

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM