资源简介
利用DirectX中的XAudio2 API所写关于在游戏中音频播放的小样例
代码片段和文件信息
#include “Utility.h“
#include “WaveFile.h“
//搜索音频文件
HRESULT FindMediaFile(WCHAR *strDestPath int Dest LPCWSTR strFilename);
//////////////////////////////////////////////////////////////////////////
// Name: Main()
// Describe:Entry point of the function
//////////////////////////////////////////////////////////////////////////
int main()
{
//1.Initialize XAudio2 初始化XAudio2
CoInitializeEx(NULL COINIT_MULTITHREADED); //为当前线程初始化COM组件
IXAudio2 *pXAudio2 = NULL;
HRESULT hr;
//创建示列
if (FAILED(hr=XAudio2Create(&pXAudio20XAUDIO2_DEFAULT_PROCESSOR)))
{
::MessageBox(NULL L“XAudio2 Create Failed“ L“Error“ NULL);
CoUninitialize(); //销毁线程
return hr;
}
//创建主声音,这是最后音频系统要通过该变量来播放声音,很关键
IXAudio2MasteringVoice *pMasterVoice = NULL;
if (FAILED(hr=pXAudio2->CreateMasteringVoice(&pMasterVoice)))
{
::MessageBox(NULL L“The Mastering voice is created failed“ L“Error“ NULL);
CoUninitialize();
return false;
}
//2.Locate the wave file 查找音频文件
WCHAR strFilePath[MAX_PATH];
if (FAILED(hr = FindMediaFile(strFilePath MAX_PATH L“Faded.wav“)))
{
return hr;
}
//3.Read in the wave file
WaveFile wav;
if (FAILED(hr = wav.Open(L“Faded.wav“ NULL WAVEFILE_READ)))
{
return hr;
}
//Get format of wave file
WAVEFORMATEX *pwfx = wav.GetFormat();
//Calculate how much bytes and samples are in the wave
DWORD cbWaveSize = wav.GetSize();
//read the sample data into memory
BYTE *pbWaveData = new BYTE[cbWaveSize];
if (FAILED(hr = wav.Read(pbWaveData cbWaveSize &cbWaveSize)))
{
SAFE_DELETE_ARRAY(pbWaveData);
return hr;
}
//4.Create a Source Voice 创建源声音,要播放的声音需要通过它来转变
IXAudio2SourceVoice *pSourceVoice;
if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice pwfx)))
{
::MessageBox(NULL L“The Source Voice is Created failed“ L“Error“ MB_OK);
return FALSE;
}
//5.Submit an XAudio2 Buffer to the source voice 将缓冲区内的音频提交给源声音
XAUDIO2_BUFFER buffer = { 0 };
buffer.pAudioData = pbWaveData;
buffer.Flags = XAUDIO2_END_OF_STREAM; //tell the source voice not to expect any data after this buffer
buffer.AudioBytes = cbWaveSize;
if (FAILED(hr = pSourceVoice->SubmitSourceBuffer(&buffer)))
{
::MessageBox(NULL L“Submit an XAudio2_Buffer Failed“ L“Error“ MB_OK);
pSourceVoice->DestroyVoice();
SAFE_DELETE_ARRAY(pbWaveData);
return FALSE;
}
//6.Play a Sound with XAudio2
wprintf_s(L“Playing Faded Wave file......“);
while (TRUE)
{
if (FAILED(hr = pSourceVoice->Start(0)))
{
return FALSE;
}
//Get the voice‘s state
BOOL isRunning = TRUE;
while (SUCCEEDED(hr) && isRunning)
{
XAUDIO2_VOICE_STATE state;
pSourceVoice->GetState(&state);
isRunning = (state.BuffersQueued > 0) != 0;
Sleep(10);
system(“pause“);
}
}
//Release all resource
pMasterVoice->DestroyVoice();
pSourceVoice->DestroyVoice();
SAFE_DELETE_ARRAY(pbWaveData);
CoUninitialize();
retu
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-10-18 17:59 DXMusic by 冷崖\
目录 0 2017-10-18 16:59 DXMusic by 冷崖\.vs\
目录 0 2017-10-18 16:59 DXMusic by 冷崖\.vs\DXMusic\
目录 0 2017-10-18 16:59 DXMusic by 冷崖\.vs\DXMusic\v14\
文件 41472 2017-10-19 17:42 DXMusic by 冷崖\.vs\DXMusic\v14\.suo
目录 0 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\
文件 7905 2017-10-19 15:23 DXMusic by 冷崖\DXMusic\DXMusic.vcxproj
文件 1247 2017-10-19 15:23 DXMusic by 冷崖\DXMusic\DXMusic.vcxproj.filters
目录 0 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\
文件 150 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.log
目录 0 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\
文件 1226 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\CL.command.1.tlog
文件 67314 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\CL.read.1.tlog
文件 774 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\CL.write.1.tlog
文件 194 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\DXMusic.lastbuildstate
文件 1188 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\li
文件 3314 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\li
文件 460 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\DXMusic.tlog\li
文件 76853 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\Main.obj
文件 73957 2017-10-19 15:24 DXMusic by 冷崖\DXMusic\Debug\WaveFile.obj
文件 1035264 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\vc140.idb
文件 708608 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Debug\vc140.pdb
文件 37518602 2017-09-30 21:27 DXMusic by 冷崖\DXMusic\Faded.wav
文件 5537 2017-10-19 15:33 DXMusic by 冷崖\DXMusic\Main.cpp
文件 565 2017-10-19 15:24 DXMusic by 冷崖\DXMusic\Utility.h
文件 15359 2017-10-19 15:23 DXMusic by 冷崖\DXMusic\WaveFile.cpp
文件 1575 2017-10-18 18:44 DXMusic by 冷崖\DXMusic\WaveFile.h
文件 1303 2017-10-18 16:59 DXMusic by 冷崖\DXMusic.sln
目录 0 2017-10-19 15:24 DXMusic by 冷崖\Debug\
文件 57344 2017-10-19 15:33 DXMusic by 冷崖\Debug\DXMusic.exe
文件 424860 2017-10-19 15:33 DXMusic by 冷崖\Debug\DXMusic.ilk
............此处省略1个文件信息
- 上一篇:SSM框架搭建亲测可用
- 下一篇:BND2-v2.0.0-windows.rar
相关资源
- 源代码
- DirectX实时渲染技术详解第二部分含源
- 红龙书最新版3d游戏开发基础DirectX1
- Introduction to 3D Game Programming with Dir
- 3d_game_programming_with_DirectX11.rar
- Real-Time_3D_Rendering_with_DirectX_and_HLSL
- Introduction to 3D Game Programming with Direc
- DirectX9.0win10安装版
- DirectX 3D HLSL精讲-随书光盘
- DirectX游戏开发终极指南pdf版+源码
- 导出DirectX .x模型插件for3DMAX2015201620
- Microsoft.DirectX.dll和Microsoft.DirectX.Dire
- Delphi DirectX8.0基于HGE中文字体实现方案
- dx9_2d_demo_game directx9实现2D游戏例程
- DirectX_web_setup.exe
- 《DirectX 11游戏编程入门》中文版+英文
- DirectX简单的贪吃蛇.rar
- PandaDirectXMaxExporter for 3DS Max 2012 64位
- Introduction to 3D Game Programming with Direc
- DirectX SDK
- 开源3D游戏引擎 HXEngine
- DirectX:3维地图的编辑
- directX方式指定声卡播放声音
- DirectX游戏
- DirectX.Capture的dll源码,dll文件,调用
- 四边形的简单绘制
- D3D11教程2源码
- dxguid.lib
- 基于DirectX+11的图形程序设计测试题及
- DirectX中文手册.pdf
评论
共有 条评论