tutorial1.cpp
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:3k
源码类别:

VC书籍

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // 文件名: play.cpp
  3. //
  4. // 描述: DirectMusic实例:显示如何在一个缺省的音频路径上播放一个片段。 
  5. //   
  6. //-----------------------------------------------------------------------------
  7. #define INITGUID
  8. #include <windows.h>
  9. #include <dmusicc.h>
  10. #include <dmusici.h>
  11. //-----------------------------------------------------------------------------
  12. // 定义,常量和全局变量
  13. //-----------------------------------------------------------------------------
  14. IDirectMusicLoader8*      g_pLoader         = NULL;
  15. IDirectMusicPerformance8* g_pPerformance    = NULL;
  16. IDirectMusicSegment8*     g_pSegment        = NULL;
  17. //-----------------------------------------------------------------------------
  18. // 函数名: WinMain()
  19. // 描 述: 在一个缺省的路径上使用DirectMusic播放一个单独的WAV文件。
  20. //-----------------------------------------------------------------------------
  21. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  22.                       INT nCmdShow )
  23. {
  24.     // 初始化COM
  25.     CoInitialize(NULL);
  26.     
  27.     // 创建装载器对象
  28.     CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
  29.                       IID_IDirectMusicLoader8, (void**)&g_pLoader );
  30.     // 创建演奏器对象
  31.     CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
  32.                       IID_IDirectMusicPerformance8, (void**)&g_pPerformance );
  33.     // 用标准的音频路径初始化演奏器。
  34.     // 这里对DirectMusic和DirectSound进行初始化, 
  35.     // 并且创建合成器。 
  36.     g_pPerformance->InitAudio( NULL, NULL, NULL, 
  37.                                DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
  38.                                DMUS_AUDIOF_ALL, NULL );
  39.     CHAR strPath[512];
  40.     if( GetWindowsDirectory( strPath, MAX_PATH+1 ) == 0 )
  41.         return 0;
  42.     strcat( strPath, "\media" );
  43.     // 告诉DirectMusic缺省的搜索路径
  44.     WCHAR wstrSearchPath[MAX_PATH+1];
  45.     MultiByteToWideChar( CP_ACP, 0, strPath, -1, 
  46.                          wstrSearchPath, MAX_PATH );
  47.     wstrSearchPath[MAX_PATH] = 0;
  48. g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes, 
  49.                                wstrSearchPath, FALSE );
  50.     
  51.     // 从文件中装载片段
  52.     WCHAR wstrFileName[MAX_PATH] = L"ding.wav";   
  53.     if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
  54.                                                IID_IDirectMusicSegment8,
  55.                                                wstrFileName,
  56.                                                (LPVOID*) &g_pSegment ) ) )
  57.     {
  58.         MessageBox( NULL, "没有发现媒体,退出采样。", 
  59.                           "DirectMusic实例", MB_OK );
  60.         g_pPerformance->CloseDown();
  61.         g_pLoader->Release(); 
  62.         g_pPerformance->Release();
  63.         CoUninitialize();
  64.         return 0;
  65.     }
  66.     // 传送片段的乐器给合成器
  67.     g_pSegment->Download( g_pPerformance );
  68.     // 播放缺省音频路径上的片段
  69.     g_pPerformance->PlaySegmentEx( g_pSegment, NULL, NULL, 0, 
  70.                                    0, NULL, NULL, NULL );
  71.     // 现在DirectMusic将在背景中播放, 
  72.     // 所有继续我们的任务
  73.     MessageBox( NULL, "点击“确定”按钮退出。", "DirectMusic实例", MB_OK );
  74.     // 停止音乐,并关闭 
  75.     g_pPerformance->Stop( NULL, NULL, 0, 0 );
  76.     g_pPerformance->CloseDown();
  77.     // 清除所有接口
  78. g_pLoader->Release(); 
  79.     g_pPerformance->Release();
  80.     g_pSegment->Release();
  81.     // 关闭COM
  82.     CoUninitialize();
  83.         
  84.     return 0;
  85. }