简体   繁体   English

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

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

How do I from C or C++ use the MP3 decoder supposedly built in with Windows since Windows Media Player 6.1? 从Windows或Windows C + C ++开始,我如何使用Windows内置的MP3解码器?

I want to play an mp3 file without having to depend on any other third party library such as for instance LAME.DLL. 我想播放一个mp3文件,而不必依赖任何其他第三方库,例如LAME.DLL。

I updated the question to better fit the answers I got, since I liked them a lot. 我更新了问题以更好地适应我得到的答案,因为我非常喜欢它们。 Related question. 相关问题。

Sure. 当然。 Like lots of other things in the Windows API, there's more than one way to go about playing .mp3 files. 与Windows API中的许多其他内容一样,播放.mp3文件的方法不止一种。 The "easiest" way to do this programmatically is using DirectShow. 以编程方式执行此操作的“最简单”方法是使用DirectShow。 The MSDN docs even include a minimal code example on a page aptly called "How To Play a File" to get you started: 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();
}

Make sure you read through the DirectShow documentation to get an idea of what's supposed to happen in a proper DirectShow application. 请务必仔细阅读DirectShow文档,以了解在正确的DirectShow应用程序中应该发生的事情。


To "feed" media data into a graph, you need to implement a IAsyncReader . 要将媒体数据“提供”到图形中,您需要实现IAsyncReader Fortunately, the Windows SDK includes a sample that implements an IAsyncReader called CAsyncReader . 幸运的是, Windows SDK包含一个实现名为CAsyncReaderIAsyncReader CAsyncReader The sample reads a media file into a memory buffer then uses CAsyncReader to stream the data into the graph. 该示例将媒体文件读入内存缓冲区,然后使用CAsyncReader将数据流式传输到图形中。 This may be what you want. 这可能就是你想要的。 On my machine the sample is located in the folder C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Samples\\multimedia\\directshow\\filters\\async . 在我的机器上,该示例位于文件夹C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Samples\\multimedia\\directshow\\filters\\async

You can control an audio channel (in order to make it play anything, including MP3) with mciSendString http://msdn.microsoft.com/en-us/library/ms709492%28VS.85%29.aspx 您可以使用mciSendString控制音频通道(以便播放任何内容,包括MP3) http://msdn.microsoft.com/en-us/library/ms709492%28VS.85%29.aspx

Here's an example (it's in C#, but it's basically the same principle): 这是一个例子(它在C#中,但它基本上是相同的原理):

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

Here is the same principle in C: 这与C中的原理相同:

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

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

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