www.pudn.com > snow.rar > PlayMp3.cpp
#include "PlayMp3.h"
// Global MP3 Data
bool g_bBackgroundMusicActive = 0;
IGraphBuilder *g_pGraph;
IMediaControl *g_pMediaControl;
IMediaEvent *g_pEvent;
IMediaSeeking *g_pSeeking;
//--------------------------------------------------------------------------------------
//
// Function to initialize the the sound graph and load
// the mp3 file
//
//--------------------------------------------------------------------------------------
bool bPlayTitleMusic()
{
HRESULT hr;
// Initialize COM
CoInitialize( NULL );
// Create the graph
CoCreateInstance( CLSID_FilterGraph,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGraphBuilder,
(void **)&g_pGraph );
// Query for interface objects
g_pGraph->QueryInterface( IID_IMediaControl, (void **)&g_pMediaControl );
g_pGraph->QueryInterface( IID_IMediaEvent, (void **)&g_pEvent );
g_pGraph->QueryInterface( IID_IMediaSeeking, (void **)&g_pSeeking );
// Load the song (insert your own file name below)
hr = g_pGraph->RenderFile(L"SnowSRC\\And_You_And_I.mp3", NULL);
if( hr != S_OK ) {
return( 0 );
}
// Set the playback rate
g_pSeeking->SetRate( 1.00 );
// Play the song
g_pMediaControl->Run();
// Set background music active flag
g_bBackgroundMusicActive = 1;
return( 1 );
}
//--------------------------------------------------------------------------------------
//
// Function to clean up the graph and allocated memory
//
//--------------------------------------------------------------------------------------
void vStopTitleMusic()
{
// Stop the song if playing
g_pMediaControl->Stop();
// Clean up
g_pMediaControl->Release();
g_pEvent->Release();
g_pGraph->Release();
}
//--------------------------------------------------------------------------------------
//
// Function to check the status of the music to see if it is done and
// needs to be restarted
//
//--------------------------------------------------------------------------------------
void vCheckMusicStatus()
{
long evCode;
// Check the event code
g_pEvent->WaitForCompletion( 0, &evCode );
// If the music is done, restart it
if( evCode == EC_COMPLETE ) {
// Set the starting position to 0
LONGLONG lStartPos = 0;
// Stop the music
g_pMediaControl->Stop();
// Set the positions
g_pSeeking->SetPositions( &lStartPos, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning );
// Run the music
g_pMediaControl->Run();
}
}
//--------------------------------------------------------------------------------------
//
// Function to cleanup allocated objects
//
//--------------------------------------------------------------------------------------
void vCleanup( void )
{
vStopTitleMusic();
}