Monday, December 14, 2009

mp3 Player in CPP Language

hi  guys!!!

have u reading the title, ya its true i have the code for mp3 player which plays the song

The code:
the header file we make as follows
mp3.h file

------------------------------------------------------------------------------------------

#ifndef _MP3H_
#define _MP3H_

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <strmif.h>
#include <Control.h>
#include <evcode.h>
#include <Uuids.h>

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

 bool    ready;

public:
cMp3();
~cMp3();

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

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

#endif

----------------------------------------------------------------------------------------------
mp3.cpp file:


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

ready = false;

CoInitialize(NULL);
}

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

void cMp3::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;
}
}

int cMp3::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;
}
}
return (ready);
}

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

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

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

Enjoy.................

1 comment:

  1. Well hw to distinguish this header file and also there is unable to file some of the included files......So what to do for that?????/

    ReplyDelete