Original Post
Below is the class I am trying to use to play music in my 2d game. It was taken from examples and pieced together into a seperate class as this is my first attempt at adding sound to a game. I included the header and .cpp file, I can get it to work when I just hardcode in what file to play, what I am looking to do is be able to send a variable to the function and play that one.
I have tried to convert the string to a WCHAR pointer and send it and it still wont work. Maybe someone who is good with directmusic would see the problem.
#pragma once
#define INITGUID
#include <windows.h>
#include <string>
#include <dmusicc.h>
#include <dmusici.h>
using namespace std;
class DMusic
{
public:
DMusic()
{
IDirectMusicLoader8* g_pLoader = NULL;
IDirectMusicPerformance8* g_pPerformance = NULL;
IDirectMusicSegment8* g_pSegment = NULL;
}
int DMusic::DMusic_Repeat(WCHAR* name);
int DMusic::Close();
private:
IDirectMusicLoader8* g_pLoader;
IDirectMusicPerformance8* g_pPerformance;
IDirectMusicSegment8* g_pSegment;
};
And here is the actual code, like I said, I can get it to work if I hard code in the file I want to play perfect, but cannot get it to accept a variable. Also if you know a better way please point it out, as I said, this is my first time using directmusic and am taking this from a straight program and trying to develop my own class.
#include "DMusic.h"
int DMusic::DMusic_Repeat(WCHAR* filename)
{
// Initialize COM
CoInitialize(NULL);
// Create loader object
CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**)&g_pLoader );
// Create performance object
CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance8, (void**)&g_pPerformance );
// Initialize the performance with the standard audio path.
// This initializes both DirectMusic and DirectSound and
// sets up the synthesizer.
g_pPerformance->InitAudio( NULL, NULL, NULL,
DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
DMUS_AUDIOF_ALL, NULL );
CHAR strPath[512];
if( GetWindowsDirectory( strPath, MAX_PATH+1 ) == 0 )
return 0;
strcat( strPath, "\\media" );
// Tell DirectMusic where the default search path is
WCHAR wstrSearchPath[MAX_PATH+1];
MultiByteToWideChar( CP_ACP, 0, strPath, -1,
wstrSearchPath, MAX_PATH );
wstrSearchPath[MAX_PATH] = 0;
g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes,
wstrSearchPath, FALSE );
WCHAR wstrFileName[50] = filename;
//WCHAR wstrFileName[MAX_PATH] = L"ding.wav";
if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
wstrFileName,
(LPVOID*) &g_pSegment ) ) )
{
MessageBox( NULL, "Media not found, sample will now quit",
"DirectMusic Tutorial", MB_OK );
g_pPerformance->CloseDown();
g_pLoader->Release();
g_pPerformance->Release();
CoUninitialize();
return 0;
}
Thanks again for anyones help, sorry for the spacing issues, I just copied and pasted it out of .net
Plex
[edited by - plexmark on June 7, 2004 3:49:12 PM]