|
最近在学习DX Music程序。。。自己看了些资料,也学着编些代码 如下
(先声明,我使用的是DEV C++ + DX8.0)
DEV C++其实就相当与没有MFC的VC。。。。容量小。相当好用
#include <windows.h>
#include <stdio.h>
#include <dmusicc.h>
#include <dmusici.h>
bool bMusic; //是否有音乐
bool bExist; //主缓冲是否已存在
bool bPlay; //是否播放
IDirectMusicPerformance* g_pPerf;
IDirectMusicLoader* g_pLoader;
IDirectMusicSegment* g_pMIDISeg;
MUSIC_TIME PauseTime; //暂停点
WCHAR MusicFile[MAX_PATH]; //文件
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
IDirectMusicPerformance* CreatePerformance(void);
IDirectMusicLoader* CreateLoader(void);
IDirectMusicSegment* LoadMIDISegment(IDirectMusicLoader* pLoader, WCHAR *wszMidiFileName );
BOOL InitMusic(); //初始化DMUSIC
BOOL LoadMusic(WCHAR *); //读入
BOOL Play(int start=0); //播放
BOOL Stop(); //停止
BOOL Pause(); //暂停
BOOL Resume(); //继续
BOOL IsPlay(); //是否正在播放
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
IDirectMusicPerformance* CreatePerformance(void)
{
IDirectMusicPerformance* pPerf;
if (FAILED(CoCreateInstance(
CLSID_DirectMusicPerformance,
NULL,
CLSCTX_INPROC,
IID_IDirectMusicPerformance,
(void**)&pPerf
)))
{
pPerf = NULL;
}
return pPerf;
}
//创建LOADER
IDirectMusicLoader* CreateLoader(void)
{
IDirectMusicLoader* pLoader;
if (FAILED(CoCreateInstance(
CLSID_DirectMusicLoader,
NULL,
CLSCTX_INPROC,
IID_IDirectMusicLoader,
(void**)&pLoader
)))
{
pLoader = NULL;
}
return pLoader;
}
//读入一段音乐
IDirectMusicSegment* LoadMIDISegment(IDirectMusicLoader* pLoader, WCHAR *wszMidiFileName )
{
if( !bMusic ) //没有音乐
return NULL;
//检测文件是否存在
FILE *fp;
char* str = Char(wszMidiFileName);
fp=fopen(str , "rb");
if( fp==NULL )
{
return NULL;
}
fclose(fp);
DMUS_OBJECTDESC ObjDesc;
IDirectMusicSegment* pSegment = NULL;
ObjDesc.guidClass = CLSID_DirectMusicSegment;
ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);
wcscpy(ObjDesc.wszFileName, wszMidiFileName );
ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME;
pLoader->GetObject(&ObjDesc,
IID_IDirectMusicSegment, (void**) &pSegment); //得到object
pSegment->SetParam(GUID_StandardMIDIFile,
-1, 0, 0, (void*)g_pPerf); //设置乐队的音轨
pSegment->SetParam(GUID_Download, -1, 0, 0, (void*)g_pPerf);//允许自动下载
return pSegment;
}
//初始化
BOOL InitMusic()
{
if(bExist) //主缓冲存在
return true;
if (SUCCEEDED(CoInitialize(NULL))) //初始化COM
{
if( !bExist ) //主缓冲不存在
{
bExist=true;
g_pPerf = CreatePerformance(); //创建主接口
if (g_pPerf == NULL)
{
MessageBox(NULL,"CreatePerformance\n初始化DirectMusic过程出错!\
请检查你是否正确的安装了DirectX 7.0以上版本,或有无其它程序正\
占用声卡?否则你将无法听到背景音乐。","ERROR",MB_OK);
bMusic=false;
return false;
}
if (FAILED(g_pPerf->Init(NULL, NULL, NULL)))
{
MessageBox(NULL,"Init\nDirectMusic 初始化出错!请安装DirectX 7.0以\
上版本。或有无其它程序正占用声卡?否则你将无法听到背景音乐。",
"ERROR",MB_OK);
bMusic=false;
return false;
}
if (FAILED(g_pPerf->AddPort(NULL))) //添加一个port
{
MessageBox(NULL,"AddPort\nDirectMusic 初始化出错!请安装DirectX 7.0以\
上版本。或有无其它程序正占用声卡?否则你将无法听到背景音乐。",
"ERROR",MB_OK);
bMusic=false;
return false;
}
g_pLoader = CreateLoader(); //创建一个loader
if (g_pLoader == NULL)
{
MessageBox(NULL,"CreatLoader\nDirectMusic 初始化出错!\
请安装DirectX 7.0以上版本。或有无其它程序正占用声卡?否则你将无法\
听到背景音乐。","ERROR",MB_OK);
bMusic=false;
return false;
}
}
bMusic=true;
return true;
}
bMusic=false;
return false;
}
//读入
BOOL LoadMusic(WCHAR *filename)
{
if( !bMusic ) //没有音乐
return true;
wcscpy( MusicFile, filename );
if (g_pLoader)
{
g_pMIDISeg = LoadMIDISegment(g_pLoader,filename); //加载一个段
return true;
}
return false;
}
//播放
BOOL Play(int start)
{
if( !bMusic ) //……
return true;
IDirectMusicSegmentState* g_pSegState; //当前段状态
if (g_pMIDISeg)
{
//播放一个段,并跟踪状态
g_pPerf-> laySegment(g_pMIDISeg, DMUS_SEGF_REFTIME , start, &g_pSegState);
}
bPlay=true;
return true;
}
//是否正在播放
BOOL IsPlay()
{
if( !bMusic ) //没有音乐
return true;
if (g_pMIDISeg)
{
if( (g_pPerf->IsPlaying(g_pMIDISeg,NULL))==S_OK )
return true;
}
return false;
}
//停止播放
BOOL Stop()
{
if( !bMusic ) //……
return true;
if (g_pMIDISeg)
{
if( (g_pPerf->Stop(g_pMIDISeg, NULL, 0, 0))==S_OK )
{
bPlay=false;
return true;
}
}
return false;
}
//暂停播放
BOOL Pause()
{
if( !bMusic ) //……
return true;
if (g_pMIDISeg)
{
g_pPerf->GetTime(NULL, &PauseTime); //播放到了什么位置
if( (g_pPerf->Stop(g_pMIDISeg, NULL, 0, 0))==S_OK ) //停止
{
bPlay=false;
return true;
}
return false;
}
return false;
}
//继续播放
BOOL Resume()
{
if( !bMusic )
return true;
if (g_pMIDISeg)
{
g_pMIDISeg->SetStartPoint(PauseTime); //开始的位置
IDirectMusicSegmentState* g_pSegState; //跟踪
g_pPerf->PlaySegment(g_pMIDISeg, DMUS_SEGF_REFTIME , 0, &g_pSegState);
g_pMIDISeg->SetStartPoint(0);
bPlay=true;
return true;
}
return false;
}
结果有很多错误啊~~暂且不考虑我程序是否可以播放。。。。但首先我编译同不过。。
错误如下:
21 C:\Dx8\include\dmusicc.h
In file included from C:/Dx8/include/dmusicc.h
3 C:\Dev-Cpp\main.cpp
from main.cpp
1 C:\Dx8\include\dmdls.h:23
[Warning] "MAKEFOURCC" redefined //就是这里,就是不知道为什么错,说什么重定义??
80 C:\Dev-Cpp\include\windows.h
In file included from C:/Dev-Cpp/include/windows.h
1 C:\Dev-Cpp\main.cpp
from main.cpp
1 C:\Dev-Cpp\include\mmsystem.h:24
[Warning] this is the location of the previous definition
21 C:\Dx8\include\dmusicc.h
In file included from C:/Dx8/include/dmusicc.h
3 C:\Dev-Cpp\main.cpp
from main.cpp
81 C:\Dx8\include\dmdls.h
declaration of `WLOOP _DMUS_REGION::WLOOP[1]'//还有这里
264 C:\Dx8\include\dls1.h
changes meaning of `WLOOP' from `typedef struct
请有经验的高手门帮我解决下问题啊!!!!谢谢了。。。。这个错误不会和我的编译器有关系吧???
|
-
|