📄 plugindialog.cpp
字号:
//
// Initialization
//////////////////////////////////////////////////////////////////////
BOOL CPluginDialog::InitMedia(HWND hWnd)
{
ChangeStateTo( Uninitialized );
hGraphNotifyEvent = NULL;
pGraph = NULL;
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// CreateFilterGraph
//////////////////////////////////////////////////////////////////////
BOOL CPluginDialog::CreateMP3FilterGraph(HWND hWnd)
{
// IMediaEventEx *pME; // Event Pointer
HRESULT hr; // for communicating with the filter graph
pGraph = NULL; // make sure we start clean
hr = CoCreateInstance(CLSID_FilterGraph, // CLSID of object
NULL, // Outer unknown
// CLSCTX_INPROC_SERVER, // Type of server
CLSCTX_INPROC,
IID_IGraphBuilder, // Interface wanted
(void **) &pGraph); // Returned object
if (FAILED(hr))
{
pGraph = NULL;
return FALSE;
}
// We use this to find out events sent by the filtergraph
hr = pGraph->QueryInterface(IID_IMediaEventEx,
(void **) &pME); // open lines of communication
if (FAILED(hr))
{
DeleteContentsMp3();
return FALSE;
}
// establish event handle
hr = pME->GetEventHandle((OAEVENT*) &hGraphNotifyEvent);
if (FAILED(hr))
{
AfxMessageBox("Failed to get Event Handle in PluginDlg");
DeleteContentsMp3();
return FALSE;
}
pME->Release();
return TRUE;
} // CreateFilterGraph
//////////////////////////////////////////////////////////////////////
// DeleteContents
//////////////////////////////////////////////////////////////////////
void CPluginDialog::DeleteContentsMp3()
{
if (pGraph != NULL)
{
pGraph->Release( );
pGraph = NULL;
}
// this event is owned by the filter graph and is thus invalid
hGraphNotifyEvent = NULL;
ChangeStateTo( Uninitialized );
} // Delete Contents
//////////////////////////////////////////////////////////////////////
// RenderFile
// Process the file through the appropriate filter path. This function is called by
// OpenMediaFile()
//////////////////////////////////////////////////////////////////////
BOOL CPluginDialog::RenderFile( LPSTR szFileName, HWND hWnd )
{
HRESULT hr = S_OK;
WCHAR wPath[MAX_PATH];
DeleteContentsMp3();
if ( !CreateMP3FilterGraph(hWnd) )
{
return FALSE;
}
MultiByteToWideChar( CP_ACP, 0, szFileName, -1, wPath, MAX_PATH );
hr = pGraph->RenderFile(wPath, NULL);
if (FAILED( hr ))
{
return FALSE;
}
hr = pGraph->QueryInterface(IID_IMediaEventEx,
(void **) &pME); // open lines of communication
if (FAILED(hr))
{
AfxMessageBox("Failed to set Query Interface pME in PluginDlg");
return FALSE;
}
pME->Release();
return TRUE;
} // RenderFile
//////////////////////////////////////////////////////////////////////
// OpenMediaFile
// This function opens and renders the specified media file.
// File..Open has been selected
//////////////////////////////////////////////////////////////////////
void CPluginDialog::OpenMediaFile( HWND hWnd, LPSTR szFile )
{
static char szFileName[ _MAX_PATH ];
if( szFile != NULL && RenderFile( szFile, hWnd)) // this calls the filter graph
{
LPSTR szTitle;
// Work out the full path name and the file name from the
// specified file
GetFullPathName( szFile, _MAX_PATH, szFileName, &szTitle );
// Set the main window title and update the state
ChangeStateTo( Stopped );
}
} // OpenMediaFile
//////////////////////////////////////////////////////////////////////
// PlayMp3
//////////////////////////////////////////////////////////////////////
void CPluginDialog::PlayMp3(long volume)
{
if( CanPlay() )
{
HRESULT hr;
// static IMediaControl *pMC;
// static IBasicAudio *pMA;
// static IMediaPosition *pMP;
// Obtain the interface to our filter graph
hr = pGraph->QueryInterface(IID_IMediaControl,
(void **) &pMC);
if( SUCCEEDED(hr) )
{
/* In order to loop sounds, we will check with Media
Player to see when the sound is almost over. When
it's within 0.05 sec of ending, we'll stop the sound,
rewind it and declare that the sound is ready to play
again. The next time around your game's main loop,
PlayMp3 will start it up again. And so it goes...
*/
// Set volume level
hr = pGraph->QueryInterface(
IID_IBasicAudio,
(void**) &pMA);
if (SUCCEEDED(hr))
{ /* Set volume.
-10000 is silence, 0 is full volume*/
hr = pMA->put_Volume(volume);
pMA->Release(); // release the interface
}
// Ask the filter graph to play and release the interface
hr = pMC->Run();
pMC->Release();
if( SUCCEEDED(hr) )
{
return;
}
}
}
} // PlayMp3
//////////////////////////////////////////////////////////////////////
// StopMp3
//////////////////////////////////////////////////////////////////////
void CPluginDialog::StopMp3()
{
HRESULT hr;
// IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = pGraph->QueryInterface(IID_IMediaControl,
(void **) &pMC);
if( SUCCEEDED(hr) )
{
pMC->Stop( ); // stop it!
pMC->Release( );
ChangeStateTo( Stopped );
}
return ;
} //StopMp3
//////////////////////////////////////////////////////////////////////
// GetGraphEventHandle
//
// We use this to check for graph events
//////////////////////////////////////////////////////////////////////
HANDLE CPluginDialog::GetGraphEventHandle()
{
return hGraphNotifyEvent;
} // GetGraphEventHandle
//////////////////////////////////////////////////////////////////////
// MP3PLAYING
// This function checks to see if the current mp3 file is still playing.
//////////////////////////////////////////////////////////////////////
geBoolean CPluginDialog::Mp3Playing()
{
HRESULT hr;
// IMediaPosition *pMP;
// query the interface
hr = pGraph->QueryInterface(
IID_IMediaPosition,
(void**) &pMP);
if (SUCCEEDED(hr))
{
REFTIME tCurrent, tLength; // find the max playtime
hr = pMP->get_Duration(&tLength);
if (SUCCEEDED(hr))
{ // where are we now?
hr = pMP->get_CurrentPosition(&tCurrent);
if (SUCCEEDED(hr))
{
// Test to see if there is any time left
while ((tLength - tCurrent) > 0)
return GE_TRUE; // if so, still playing, buddy.
}
}
} // when done playing...
pMP->Release(); // release our access to the interface
return GE_FALSE;// mp3 file is all done playing.
} //Mp3Playing
///////////////////////////////////////////////////////////////////////////////
// StopBackgroundMp3
///////////////////////////////////////////////////////////////////////////////
void CPluginDialog::StopBackgrndMp3(HWND m_hWnd)
{
if (Mp3Playing())
{
StopMp3(); // kill the mp3 and free some memory
}
DeleteContentsMp3(); // Delete the contents from memory
UnInitmp3Mgr(m_hWnd); // Uninitialize Mp3Mgr. It's over, Man.
Initialized = false; // mp3 is over
}
///////////////////////////////////////////////////////////////////////////////
// DoBackgroundMp3
///////////////////////////////////////////////////////////////////////////////
void CPluginDialog::DoBackgrndMp3(HWND m_hWnd, char *backgrndmp3, long volume)
{
if (Initialized == false)
{
mp3Mgr_Create(m_hWnd); // Initialize Mp3Mgr
}
OpenMediaFile(m_hWnd, backgrndmp3); // spec your own *.mp3
// NOTE: -10000 is silence, 0 is full volume.
// FALSE plays once
// TRUE loops continuously
// vol loop
PlayMp3(volume);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -