This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.


  Playing An MP3 With DirectShow
  Submitted by

This is my submission for code of the day. Its a class that wraps up using DirectShow to play an mp3. To use this you will need to have the DirectMedia SDK installed. You can use this code wherever and whenever you like, I hope someone finds it helpfull.

Download Associated File: mp3.h (1,465 bytes)

// MP3.h

#include <windows.h>
#include <mmsystem.h>
#include <streams.h>

class Mp3 { private: IBaseFilter * pif; IGraphBuilder * pigb; IMediaControl * pimc; IMediaEventEx * pimex;

bool ready;

public: Mp3(); ~Mp3();

void Load(LPSTR filename); void Cleanup();

void Play(); void Pause(); void Stop(); };

// MP3.cpp #include "Mp3.h"

Mp3::Mp3() { pif = NULL; pigb = NULL; pimc = NULL; pimex = NULL;

ready = false;

CoInitialize(NULL); }

Mp3::~Mp3() { Cleanup(); }

void Mp3::Cleanup() { CoUninitialize();

if (pimc) pimc->Stop();

if(pif) { pif->Release(); pif = NULL; }

if(pigb) { pigb->Release(); pigb = NULL; }

if(pimc) { pimc->Release(); pimc = NULL; }

if(pimex) { pimex->Release(); pimex = NULL; } }

void Mp3::Load(LPSTR szFile) { WCHAR wFile[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, szFile, -1, wFile, MAX_PATH);

if (SUCCEEDED(CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&this->pigb))) { pigb->QueryInterface(IID_IMediaControl, (void **)&pimc); pigb->QueryInterface(IID_IMediaEventEx, (void **)&pimex);

if (SUCCEEDED(pigb->RenderFile(wFile, NULL))) { ready = true; } } }

void Mp3::Play() { if (ready) { pimc->Run(); } }

void Mp3::Pause() { if (ready) { pimc->Pause(); } }

void Mp3::Stop() { if (ready) { pimc->Stop(); } }

//Alan //----- //

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.