Windows平台下的MP3文件载入与播放

最近我学习了在Windows平台上进行游戏开发时播放MP3文件的相关知识。MP3文件由于其闭源性质,直接解码非常困难。幸运的是,DirectShow解决了这一难题。我们只需包含头文件并手动链接库文件即可使用其函数。为此,我们必须加入头文件dshow.h。由于新版DirectX中已不再包含DirectShow,因此我下载了2004年12月的DirectX版本,该版本中包含了dshow.h。加入头文件后,需要用到以下接口:IGraphBuilder、IMediaControl、IMediaEventEx。此外,还需手动链接Strmiids.lib库。准备工作完成后,我们可以开始编写一个类CMP3。该类中嵌入了另一个MP3标签类CMP3Tag。以下是该类的代码:

class CMP3 {
public:
    CMP3(void) : m_GraphBuilder(nullptr), m_MediaControl(nullptr), m_MediaEventEx(nullptr), m_IsPlaying(false) {}
    ~CMP3(void);
    bool LoadMP3File(TString strFilename);
    bool Play(void);
    void ProcessNotification(void);
    CMP3Tag GetMP3Tag(void) { return m_tag; }
    bool IsPlaying(void) { return m_IsPlaying; }
private:
    bool m_IsPlaying;
    IGraphBuilder* m_GraphBuilder;
    IMediaControl* m_MediaControl;
    IMediaEventEx* m_MediaEventEx;
    CMP3Tag m_tag;
};
rar 文件大小:2.7MB