here is the code (it''s from 2kings a bit modified)
when I run the program at the first time, all is good, I can hear the sound file
but when I quit the program and run it again, the computer crash, and I need to restart computer...
here is the code, a bit long, but
please help me!
class CSound
{
public:
void Load(CAudio Audio,char *file);
void Play();
void Stop();
bool IsPlaying();
void ShutDownSound();
private:
IDirectMusicPerformance8* Performance;
IDirectMusicLoader8* Loader;
IDirectMusicSegment8* Segment;
};
void CSound::Load(CAudio Audio,char *file)
{
WCHAR wcFile[MAX_PATH];
Segment=NULL;
Performance=Audio.GetPerformance();
Loader=Audio.GetLoader();
CoCreateInstance(CLSID_DirectMusicSegment,NULL,CLSCTX_INPROC,IID_IDirectMusicSegment8,(void**)&Segment);
MultiByteToWideChar(CP_ACP,0,file,-1,wcFile,MAX_PATH);
Loader->LoadObjectFromFile(CLSID_DirectMusicSegment,IID_IDirectMusicSegment8,wcFile,(void**)&Segment);
Segment->Download(Performance);
}
void CSound::Play()
{
Performance->PlaySegmentEx(Segment,0,NULL,DMUS_SEGF_SECONDARY,0,0,NULL,NULL);
}
void CSound::Stop()
{
Performance->StopEx(Segment,0,0);
}
bool CSound::IsPlaying()
{
return (Performance->IsPlaying(Segment,NULL) == S_OK);
}
void CSound::ShutDownSound()
{
Segment->Unload(Performance);
Segment->Release();
CoUninitialize();
}
class CAudio
{
public:
void InitAudio();
void SetFilesDirectory();
void ShutDownAutdio();
void SetMasterVolume(long volume);
inline IDirectMusicPerformance8* GetPerformance()
{
return Performance;
}
inline IDirectMusicLoader8* GetLoader()
{
return Loader;
}
private:
IDirectMusicPerformance8 *Performance;
IDirectMusicLoader8 *Loader;
};
void CAudio::InitAudio()
{
CoInitialize(NULL);
CoCreateInstance(CLSID_DirectMusicLoader,NULL,CLSCTX_INPROC,IID_IDirectMusicLoader8,(void**)&Loader);
CoCreateInstance(CLSID_DirectMusicPerformance,NULL,CLSCTX_INPROC,IID_IDirectMusicPerformance8,(void**)&Performance);
Performance->InitAudio(NULL,NULL,NULL,DMUS_APATH_DYNAMIC_STEREO,64,DMUS_AUDIOF_ALL,NULL);
SetFilesDirectory();
}
void CAudio::SetFilesDirectory()
{
char szSearchPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH,szSearchPath);
strcat(szSearchPath,"//sounds");
WCHAR wszSearchPath[MAX_PATH];
MultiByteToWideChar(CP_ACP,0,szSearchPath,-1,wszSearchPath,MAX_PATH);
Loader->SetSearchDirectory(GUID_DirectMusicAllTypes,wszSearchPath,false);
}
void CAudio::ShutDownAutdio()
{
Performance->Stop(NULL,NULL,0,0);
Loader->Release();
Performance->CloseDown();
Performance->Release();
CoUninitialize();
}
void CAudio::SetMasterVolume(long volume)
{
Performance->SetGlobalParam(GUID_PerfMasterVolume,&volume,sizeof(long));
}