#include "stdhdr.h"
#include "MP3.h"
#include "Window.h"
#include "Debug.h"
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "amstrmid.lib")
IGraphBuilder *g_pGraphBuilder;
IMediaControl *g_pMediaControl;
IMediaSeeking *g_pMediaSeeking;
IMediaEventEx *g_pMediaEventEx;
BOOL InitMP3()
{
HRESULT h;
::CoInitialize(NULL);
h = CoCreateInstance(CLSID_FilterGraph, NULL,
CLSCTX_INPROC, IID_IGraphBuilder,
(void **)&g_pGraphBuilder);
if (FAILED(h))
{
DISPLAYERRORCODE(h, "DirectShow could not be initialised. DirectX 8 may not be installed.");
return FALSE;
}
h = g_pGraphBuilder->QueryInterface(IID_IMediaControl,
(void **)&g_pMediaControl);
if (FAILED(h))
{
DISPLAYERRORCODE(h, "DirectShow could not be initialised. DirectX 8 may not be installed.");
return FALSE;
}
h = g_pGraphBuilder->QueryInterface(IID_IMediaSeeking,
(void **)&g_pMediaSeeking);
if (FAILED(h))
{
DISPLAYERRORCODE(h, "DirectShow could not be initialised. DirectX 8 may not be installed.");
return FALSE;
}
h = g_pGraphBuilder->QueryInterface(IID_IMediaEventEx,
(void **)&g_pMediaEventEx);
if (FAILED(h))
{
DISPLAYERRORCODE(h, "DirectShow could not be initialised. DirectX 8 may not be installed.");
return FALSE;
}
g_pMediaEventEx->SetNotifyWindow((OAHWND)hWndMain, WM_APP, 0);
return TRUE;
}
IBaseFilter *LoadMP3(LPCWSTR wszFilename)
{
IBaseFilter *pSource;
HRESULT h;
h = g_pGraphBuilder->AddSourceFilter(wszFilename, wszFilename, &pSource);
if (FAILED(h))
return NULL;
return pSource;
}
void PlayMP3(IBaseFilter *pSource)
{
IPin *pPin = NULL;
IEnumFilters *pFilterEnum = NULL;
IBaseFilter **ppFilters;
IBaseFilter *pFilterTemp = NULL;
int iFiltCount;
int iPos;
pSource->FindPin(L"Output", &pPin);
g_pMediaControl->Stop();
g_pGraphBuilder->EnumFilters(&pFilterEnum);
for (iFiltCount = 0; pFilterEnum->Skip(1) == S_OK; iFiltCount++)
;
ppFilters = (IBaseFilter **)_alloca(sizeof(IBaseFilter *) * iFiltCount);
pFilterEnum->Reset();
for (iPos = 0; pFilterEnum->Next(1, &ppFilters[iPos], NULL) == S_OK; iPos++)
;
pFilterEnum->Release();
for (iPos = 0; iPos < iFiltCount; iPos++)
{
g_pGraphBuilder->RemoveFilter(ppFilters[iPos]);
g_pGraphBuilder->AddFilter(ppFilters[iPos], NULL);
ppFilters[iPos]->Release();
}
g_pGraphBuilder->Render(pPin);
pPin->Release();
LONGLONG llPos = 0;
g_pMediaSeeking->SetPositions(&llPos, AM_SEEKING_AbsolutePositioning,
&llPos, AM_SEEKING_NoPositioning);
g_pMediaControl->Run();
}
void ReplayMP3()
{
if (g_pMediaControl != NULL)
{
LONGLONG llPos = 0;
g_pMediaSeeking->SetPositions(&llPos, AM_SEEKING_AbsolutePositioning,
&llPos, AM_SEEKING_NoPositioning);
g_pMediaControl->Run();
}
}
void StopMP3()
{
if (g_pMediaControl != NULL)
g_pMediaControl->Stop();
}
void ExitMP3()
{
if (g_pMediaEventEx != NULL)
{
g_pMediaEventEx->Release();
g_pMediaEventEx = NULL;
}
if (g_pMediaSeeking != NULL)
{
g_pMediaSeeking->Release();
g_pMediaSeeking = NULL;
}
if (g_pMediaControl != NULL)
{
g_pMediaControl->Release();
g_pMediaControl = NULL;
}
if (g_pGraphBuilder != NULL)
{
g_pGraphBuilder->Release();
g_pGraphBuilder = NULL;
}
}
void HandleMP3Events()
{
long evCode, param1, param2;
HRESULT h;
for (;;)
{
h = g_pMediaEventEx->GetEvent(&evCode, ¶m1, ¶m2, 0);
if (FAILED(h))
return;
g_pMediaEventEx->FreeEventParams(evCode, param1, param2);
OnMP3Finish(evCode);
}
}