I am trying to play a pcm udp audio stream on windows8. Is there an easier way than doing xaudio2 ?
I am very new to xaudio2 and creating an xaudio player is throwing an exception for me :
public ref class Player sealed
{
public:
void feedData(Platform::Array<unsigned char> ^byteArray)
{
buffer.AudioBytes = byteArray->Length;
buffer.pAudioData = new byte[byteArray->Length];
memcpy(buffer.pAudioData, &byteArray[0], byteArray->Length);
if( FAILED(hr = SourceVoice->SubmitSourceBuffer( &buffer ) ) )
throw Platform::Exception::CreateException(hr);
}
Player()
{
HRESULT hr;
if ( FAILED(hr = XAudio2Create( &XAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) )
throw Platform::Exception::CreateException(hr);
if ( FAILED(hr = XAudio2->CreateMasteringVoice( &MasterVoice ) ) )
throw Platform::Exception::CreateException(hr);
ZeroMemory(&wfx,sizeof(WAVEFORMATEXTENSIBLE));
wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
wfx.Format.nChannels = 1;
wfx.Format.nSamplesPerSec = 16000;
wfx.Format.nAvgBytesPerSec = 32000;
wfx.Format.nBlockAlign = 2;
wfx.Format.wBitsPerSample = 16;
if( FAILED(hr = XAudio2->CreateSourceVoice( &SourceVoice, (WAVEFORMATEX*)&wfx ) ))
throw Platform::Exception::CreateException(hr);
if ( FAILED(hr = SourceVoice->Start( 0 ) ) )
throw Platform::Exception::CreateException(hr);
}
~Player()
{
MasterVoice->DestroyVoice();
SourceVoice->DestroyVoice();
}
private:
Microsoft::WRL::ComPtr<IXAudio2> XAudio2;
IXAudio2MasteringVoice* MasterVoice;
IXAudio2SourceVoice* SourceVoice;
WAVEFORMATEXTENSIBLE wfx;
XAUDIO2_BUFFER buffer;
};
I am running it as a WinRT component dll and the exception occurs in this line:
if( FAILED(hr = XAudio2->CreateSourceVoice( &SourceVoice, (WAVEFORMATEX*)&wfx ) ))
throw Platform::Exception::CreateException(hr);
I stepped through the debugger and the wfx and SourceVoice structures look initiated okay. Can someone help me in figuring out what is going wrong ?