📄 myplaymidi.h
字号:
//************************************************************
//播放Midi模块
//作者:曾铮
//时间:2004年9月
//说明:用于游戏中播放Midi。
//************************************************************
#ifndef MyPlayMidi_h_
#define MyPlayMidi_h_
#define INITGUID
#include <dmusicc.h>
#include <dmusici.h>
//-----------------------------------------------------------------------------
// 全局变量定义
//-----------------------------------------------------------------------------
IDirectMusicLoader8* g_pLoader = NULL;
IDirectMusicPerformance8* g_pPerformance = NULL;
IDirectMusicSegment8* g_pSegment = NULL;
bool isPlayingMidi=true;//初始化为有音乐
//文件目录
CHAR strPath[512];
void PlayMidiInit()
{
// 初始化 COM
CoInitialize(NULL);
// 创建 loader 对象
CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**)&g_pLoader );
// 创建 performance 对象
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.
//用标准音频路径初始化performance,并初始化DirectMusic和DirectSound,建立synthesizer
g_pPerformance->InitAudio( NULL, NULL, NULL,
DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
DMUS_AUDIOF_ALL, NULL );
GetCurrentDirectory(MAX_PATH,strPath);//获取当前程序目录
// 告诉 DirectMusic 默认搜索路径
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 );
return;
}
bool PlayMidi_1()
{
HRESULT hr;
// Load the segment from the file
WCHAR wstrFileName[MAX_PATH] = L"\\sound\\bkm1.mid";
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 false;
}
// Download the segment's instruments to the synthesizer
g_pSegment->Download( g_pPerformance );
// Set the segment to repeat many times
//if( FAILED( hr = g_pSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
// return false;
// Play segment on the default audio path
g_pPerformance->PlaySegmentEx( g_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL );
///////////////////////////////////////////////////////////////////////////////
WCHAR wstrFileName1[MAX_PATH] = L"\\sound\\bkm2.mid";
if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
wstrFileName1,
(LPVOID*) &g_pSegment ) ) )
{
MessageBox( NULL, "Media not found, sample will now quit", "DirectMusic Tutorial", MB_OK );
return false;
}
// Download the segment's instruments to the synthesizer
g_pSegment->Download( g_pPerformance );
// Set the segment to repeat many times
if( FAILED( hr = g_pSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
return false;
// Play segment on the default audio path
g_pPerformance->PlaySegmentEx( g_pSegment, NULL, NULL, DMUS_SEGF_SEGMENTEND, 0, NULL, NULL, NULL );
return true;
}
bool PlayMidi_2()
{
// HRESULT hr;
// Load the segment from the file
WCHAR wstrFileName[MAX_PATH] = L"\\sound\\bkm2.mid";
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 false;
}
// Download the segment's instruments to the synthesizer
g_pSegment->Download( g_pPerformance );
// Set the segment to repeat many times
// if( FAILED( hr = g_pSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
// return false;
// Play segment on the default audio path
g_pPerformance->PlaySegmentEx( g_pSegment, NULL, NULL, 0,
0, NULL, NULL, NULL );
return true;
}
bool PlayBGMusic()//播放背景音乐
{
PlayMidi_1();
// PlayMidi_2();
return true;
}
void StopMidi()
{
// Stop the music
g_pPerformance->Stop( NULL, NULL, 0, 0 );
return;
}
void FreeMidi()
{
//close down
g_pPerformance->CloseDown();
// Cleanup all interfaces
g_pLoader->Release();
g_pPerformance->Release();
g_pSegment->Release();
// Close down COM
CoUninitialize();
return;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -