1

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 ?

pkumar0
  • 2,069
  • 3
  • 14
  • 21
  • ther error code comes out to be -2003435519 or 0x7769FFFF . I Cannot find anything in the event viewer either. – pkumar0 Mar 27 '12 at 21:13
  • Sorry, found the issue. wfx.Format.wFormatTag needs to be WAVE_FORMAT_PCM if channel is 1 or 2 irrespective of whether the structure is defined as WAVEFORMATEXTENSIBLE. – pkumar0 Mar 27 '12 at 21:54
  • Could you post your solution as an answer? – jv42 Mar 28 '12 at 07:52
  • Edited the code above with the solution. – pkumar0 Mar 28 '12 at 19:33
  • What I meant is: on StackOverflow, we try to have the 'questions' answered by 'answers', it's much easier to read than parsing comments. – jv42 Mar 29 '12 at 08:49

0 Answers0