一個DirectX8的網站 |
|
jackkcg
站務副站長 發表:891 回覆:1050 積分:848 註冊:2002-03-23 發送簡訊給我 |
http://www.andypike.com/Tutorials/DirectX8/
一個DirectX8的網站 A new class - CSound
Now that we have created and initialised DirectX Audio, we can load and play sounds and music. To help us with this, I have created a new class called CSound which is shown below. Take a quick look at the methods, and I'll explain how it works.
CSound::CSound()
{
m_pDirectAudioPerformance = NULL;
m_pDirectAudioLoader = NULL;
m_pSegment = NULL;
m_pGraph = NULL;
m_pMediaControl = NULL;
m_pMediaPosition = NULL;
m_enumFormat = Unknown; LogInfo("[*]Sound created OK");
} void CSound::InitialiseForWavMidi(IDirectMusicPerformance8* pDirectAudioPerformance, IDirectMusicLoader8* pDirectAudioLoader)
{
m_pDirectAudioPerformance = pDirectAudioPerformance;
m_pDirectAudioLoader = pDirectAudioLoader; m_enumFormat = WavMidi;
} void CSound::InitialiseForMP3()
{
CoCreateInstance(CLSID_FilterGraph, NULL,
CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph); m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl); m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition); m_enumFormat = MP3;
} CSound::~CSound()
{
Stop(); SafeRelease(m_pSegment);
SafeRelease(m_pGraph);
SafeRelease(m_pMediaControl);
SafeRelease(m_pMediaPosition); LogInfo("[*]Sound destroyed OK");
} bool CSound::LoadSound(const char* szSoundFileName)
{
WCHAR wstrSoundPath[MAX_PATH];
CHAR strSoundPath[MAX_PATH]; switch(m_enumFormat)
{
case MP3:
//Get the our applications "sounds" directory.
GetCurrentDirectory(MAX_PATH, strSoundPath);
strcat(strSoundPath, "\\Sounds\\");
strcat(strSoundPath, szSoundFileName); //Convert the path to unicode.
MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH); m_pGraph->RenderFile(wstrSoundPath, NULL);
break;
case WavMidi:
//Convert the filename to unicode.
MultiByteToWideChar(CP_ACP, 0, szSoundFileName, -1, wstrSoundPath, MAX_PATH); //Load a sound
m_pDirectAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8,
wstrSoundPath, (void**) &m_pSegment); m_pSegment->Download(m_pDirectAudioPerformance);
break;
default:
return false;
} return true;
} bool CSound::Play(DWORD dwNumOfRepeats)
{ switch(m_enumFormat)
{
case MP3:
//Make sure that we are at the start of the stream
m_pMediaPosition->put_CurrentPosition(0);
//Play mp3
m_pMediaControl->Run();
break;
case WavMidi:
//Set the number of times the sound repeats
m_pSegment->SetRepeats(dwNumOfRepeats); //To loop the sound forever, pass in DMUS_SEG_REPEAT_INFINITE //Play the loaded sound
m_pDirectAudioPerformance->PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL);
break;
default:
return false;
} return true;
} bool CSound::Stop()
{
switch(m_enumFormat)
{
case MP3:
m_pMediaControl->Stop();
break;
case WavMidi:
//Stop the loaded sound
m_pDirectAudioPerformance->StopEx(m_pSegment, 0, 0);
break;
default:
return false;
} return true;
} bool CSound::IsPlaying()
{
switch(m_enumFormat)
{
case MP3:
REFTIME refPosition;
REFTIME refDuration; m_pMediaPosition->get_CurrentPosition(&refPosition);
m_pMediaPosition->get_Duration(&refDuration);
if(refPosition < refDuration)
{
return true;
}
else
{
return false;
}
break;
case WavMidi:
if(m_pDirectAudioPerformance->IsPlaying(m_pSegment, NULL) == S_OK)
{
return true;
}
else
{
return false;
}
break;
default:
return false;
}
}
可以參考看看
------
********************************************************** 哈哈&兵燹 最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好 Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知 K.表Knowlege 知識,就是本站的標語:Open our mind |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |