Hi to everyone! I need your help with this because my knowledge of vectors seems to get smaller and smaller everyday (the more I learn the less I know).
I have a class that initializes directx sound objects like this:
//header
#ifndef _CSOUND_H
#define _CSOUND_H
#include "CAudio.h"
class CSound {
private:
IDirectMusicPerformance8* m_pPerformance;
IDirectMusicLoader8* m_pLoader;
IDirectMusicSegment8* m_pSegment;
public:
CSound(char* szFilename_, IDirectMusicPerformance8 *pPerformance,
IDirectMusicLoader8 *pLoader);
~CSound();
void Play();
void Stop();
void SetRepeats(DWORD);
bool IsPlaying();
};
#endif
//.cpp
#include "CSound.h"
CSound::CSound(char *szFilename_, IDirectMusicPerformance8 *pPerformance,
IDirectMusicLoader8 *pLoader)
{
WCHAR wszFile[MAX_PATH];
m_pSegment = NULL;
m_pPerformance = pPerformance;
m_pLoader = pLoader;
//Create segment
CoCreateInstance(CLSID_DirectMusicSegment, NULL, CLSCTX_INPROC, IID_IDirectMusicSegment8,
(void**)&m_pSegment);
//Load segment from file
MultiByteToWideChar(CP_ACP, 0, szFilename_, -1, wszFile, MAX_PATH);
if (FAILED(m_pLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8, wszFile,
(void**)&m_pSegment))) {
MessageBox(NULL, "Failed to load sound", "CSound", MB_OK | MB_ICONEXCLAMATION);
}
//Download band
m_pSegment->Download(m_pPerformance);
}
CSound::~CSound()
{
if (m_pSegment) {
m_pSegment->Unload(m_pPerformance);
m_pSegment->Release();
m_pSegment = NULL;
}
}
void CSound:lay()
{
m_pPerformance->PlaySegmentEx(m_pSegment, 0, NULL,
DMUS_SEGF_SECONDARY, 0, 0, NULL, NULL);
}
void CSound::Stop()
{
m_pPerformance->StopEx(m_pSegment,0,0);
}
void CSound::SetRepeats(DWORD dwRepeats)
{
//A value of DMUS_SEG_REPEAT_INFINITE means infinite
m_pSegment->SetRepeats(dwRepeats);
}
bool CSound::IsPlaying()
{
return (m_pPerformance->IsPlaying(m_pSegment, NULL) == S_OK);
}
The code is based from this
http://www.riaz.de/tutorials/dxaudio02/dxaudio02.html
site.
I''ve tested the code creating a normal istance of my class and it all works fine. (Using Win2K and VC++ 6.0)
But what I would like to do is push the sound object instances in a vector container. Currently I "do" it like this:
m_cAudio = new CAudio(GetHWND());
m_cAudio->SetMasterVolume(500);
m_cSounds.push_back(CSound("perdu.wav",
m_cAudio->GetPerformance(),
m_cAudio->GetLoader()));
m_cSounds.push_back(CSound("gagne.wav",
m_cAudio->GetPerformance(),
m_cAudio->GetLoader()));
The problem is that it doesn''t work. (using the same method to fill a vector using another class I''ve created works just fine).
If I''ve understood correctly the object is created and then destroyed. (Using a breakpoint, I see that the destructor is called at the "push_back".) So it actually closes down the channel before I get to use it (leading to exceptions and such).
What do you propose I should do?
Move the destructor stuff to another function? Is there another way to do it properly?
Thank you for your time!