Its actually pretty easy to use. Here's some code for playing a song as requested.
First you need to add an FMUSIC_MODULE member to your class definition.
- Code:
FMUSIC_MODULE * m_module;
Then in your InitInstance method you'll need to init the sound system.
- Code:
DWORD retry = 0;
while(!FSOUND_Init(32000, 320, 0) && retry < 10) { Sleep(100); retry++; }
if(retry == 10) return E_FAIL;
m_module = NULL;
You'll want to make sure you clean up the sound system in the ExitInstance method as well.
- Code:
if(m_module) FMUSIC_FreeSong(m_module);
FSOUND_Close();
Load your song resource and set your volume for that particular song in the CreateSurfaces method. In this case we're loading the song filespec from a string table.
- Code:
int r = LoadString(m_hInst, IDS_FILE_MUSIC_1, file, sizeof(file)/sizeof(TCHAR)-1);
if(r == 0) return E_FAIL;
m_module = FMUSIC_LoadSong(c_getFullPath(file));
if(!m_module) return E_FAIL;
if(!FMUSIC_SetMasterVolume(m_module, 64)) return E_FAIL;
Here's the code for the c_getFullPath method. You have to pass a char * to FMUSIC_LoadSong, not a TCHAR *, so this fixes that up.
- Code:
char * CMyApplication::c_getFullPath(TCHAR *filename)
{
static TCHAR lpFilename[1024];
static char cFilename[1024];
INT nStrLen;
TCHAR *pDest;
INT result;
INT ch = '\\';
for(INT i = 0; i < 1024; i++) lpFilename[i] = '\0';
nStrLen = GetModuleFileName(NULL, lpFilename, 1024);
pDest = _tcsrchr(lpFilename, ch);
if (pDest != NULL) {
result = pDest - lpFilename + 1;
lpFilename[result] = '\0';
if (filename != NULL) _tcscat(lpFilename, filename);
#ifndef _WIN32_WCE
return lpFilename;
#else
size_t n = wcstombs(cFilename, lpFilename, 1024 - 1);
return cFilename;
#endif
} else return NULL;
}
Finally, you can play your song.
- Code:
FMUSIC_PlaySong(m_module);