gpt4 book ai didi

c++ - 如何在XAudio2上同时播放多种声音?

转载 作者:行者123 更新时间:2023-12-03 07:25:58 24 4
gpt4 key购买 nike

我目前正在尝试使用XAudio2在Windows中制作一个游戏应用程序,但我不知道如何在播放声音时使该应用程序不阻塞。我尝试在this repository.中的示例中调用新线程

但这只会导致错误。我尝试在函数中传递对母带声音的引用,但随后它仅引发“XAudio2:必须先创建母带声音”错误。我想念什么吗?我只是想让它一次播放两个声音并从那里建立声音。我查看了文档,但是它非常模糊。

最佳答案

XAudio2是一个非阻塞API。要同时播放两种声音,您至少需要两种“源声音”和一种“母带声音”。

DX::ThrowIfFailed(
CoInitializeEx( nullptr, COINIT_MULTITHREADED )
);

Microsoft::WRL::ComPtr<IXAudio2> pXAudio2;
// Note that only IXAudio2 (and APOs) are COM reference counted
DX::ThrowIfFailed(
XAudio2Create( pXAudio2.GetAddressOf(), 0 )
);

IXAudio2MasteringVoice* pMasteringVoice = nullptr;
DX::ThrowIfFailed(
pXAudio2->CreateMasteringVoice( &pMasteringVoice )
);

IXAudio2SourceVoice* pSourceVoice1 = nullptr;
DX::ThrowIfFailed(
pXaudio2->CreateSourceVoice( &pSourceVoice1, &wfx ) )
// The default 'pSendList' will be just to the pMasteringVoice
);

IXAudio2SourceVoice* pSourceVoice2 = nullptr;
DX::ThrowIfFailed(
pXaudio2->CreateSourceVoice( &pSourceVoice2, &wfx) )
// Doesn't have to be same format as other source voice
// And doesn't have to match the mastering voice either
);

DX::ThrowIfFailed(
pSourceVoice1->SubmitSourceBuffer( &buffer )
);

DX::ThrowIfFailed(
pSourceVoice2->SubmitSourceBuffer( &buffer /* could be different WAV data or not */)
);

DX::ThrowIfFailed(
pSourceVoice1->Start( 0 );
);

DX::ThrowIfFailed(
pSourceVoice2->Start( 0 );
);

You should take a look at the samples on GitHub as well as DirectX Tool Kit for Audio



如果您想确保两种声音都在同一时间开始,则可以使用:
DX::ThrowIfFailed(
pSourceVoice1->Start( 0, 1 );
);

DX::ThrowIfFailed(
pSourceVoice2->Start( 0, 1 );
);

DX::ThrowIfFailed(
pSourceVoice2->CommitChanges( 1 );
);

关于c++ - 如何在XAudio2上同时播放多种声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49279982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com