在Metro XAudio2和Windows地铁上播放声音会引发异常

时间:2012-03-27 01:35:27

标签: windows-8 mediaelement xaudio2

我正在尝试在windows8上播放pcm udp音频流。有没有比做xaudio2更简单的方法?

我是xaudio2的新手,创建一个xaudio播放器给我一个例外:

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

我将它作为WinRT组件dll运行,并且此行发生异常:

if( FAILED(hr = XAudio2->CreateSourceVoice( &SourceVoice, (WAVEFORMATEX*)&wfx ) ))
    throw Platform::Exception::CreateException(hr);

我介入了调试器,wfx和SourceVoice结构看起来没问题。有人可以帮我弄清楚出了什么问题吗?

0 个答案:

没有答案