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

发布于 2024-12-15 09:29:42 字数 315 浏览 3 评论 0 原文

我如何在 C 或 C++ 中使用自 Windows Media Player 6.1 以来 Windows 内置的 MP3 解码器?

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

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

How do I from C or C++ use the MP3 decoder supposedly built in with Windows since Windows Media Player 6.1?

I want to play an mp3 file without having to depend on any other third party library such as for instance LAME.DLL.

I updated the question to better fit the answers I got, since I liked them a lot. Related question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-12-22 09:29:42

当然。与 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 包含一个示例,它实现了一个名为的 IAsyncReader CAsyncReader。该示例将媒体文件读入内存缓冲区,然后使用 CAsyncReader 将数据流式传输到图表中。这可能就是你想要的。在我的计算机上,示例位于文件夹 C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\multimedia\directshow\filters\async 中。

Sure. Like lots of other things in the Windows API, there's more than one way to go about playing .mp3 files. The "easiest" way to do this programmatically is using DirectShow. The MSDN docs even include a minimal code example on a page aptly called "How To Play a File" to get you started:

// 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.


To "feed" media data into a graph, you need to implement a IAsyncReader. Fortunately, the Windows SDK includes a sample that implements an IAsyncReader called CAsyncReader. The sample reads a media file into a memory buffer then uses CAsyncReader to stream the data into the graph. 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.

琉璃繁缕 2024-12-22 09:29:42

您可以使用 mciSendString 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中也是同样的原理:

< a href="http://www.daniweb.com/software-development/c/code/268167" rel="nofollow">http://www.daniweb.com/software-development/c/code/268167

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

Here's an example (it's in C#, but it's basically the same principle):

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

Here is the same principle in C:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文