1

我目前正在尝试使用 XAudio2 在 Windows 中制作游戏应用程序,但我无法弄清楚如何使应用程序在播放声音时不被阻塞。我尝试在此存储库的示例中调用一个新线程。

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

4

2 回答 2

3

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 );
);

您应该查看GitHub 上的示例以及DirectX Tool Kit for Audio

如果您想确保两个源声音同时开始,您可以使用:

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

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

DX::ThrowIfFailed(
    pSourceVoice2->CommitChanges( 1 );
);
于 2018-03-15T05:27:38.237 回答
0

如果您想同时播放多个声音,请拥有一个playSound功能并启动各种线程来播放您的各种声音,每个声音都有一个特定的源声音。

XAudio2 将负责将每个声音映射到可用通道(或者,如果您有更高级的系统,您可以使用自己指定映射IXAudio2Voice::SetOutputMatrix)。

void playSound( IXAudio2SourceVoice* sourceVoice )
{
    BOOL isPlayingSound = TRUE;
    XAUDIO2_VOICE_STATE soundState = {0};
    HRESULT hres = sourceVoice->Start( 0u );
    while ( SUCCEEDED( hres ) && isPlayingSound )
    {// loop till sound completion
        sourceVoice->GetState( &soundState );
        isPlayingSound = ( soundState.BuffersQueued > 0 ) != 0;
        Sleep( 100 );
    }
}

例如同时播放两个声音:

IXAudio2SourceVoice* pSourceVoice1 = nullptr;
IXAudio2SourceVoice* pSourceVoice2 = nullptr;

// setup the source voices, load the sounds etc..

std::thread thr1{ playSound, pSourceVoice1 };
std::thread thr2{ playSound, pSourceVoice2 };

thr1.join();
thr2.join();
于 2020-10-23T12:01:55.057 回答