Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 8 在Metro XAudio2和windows Metro上播放声音会引发异常_Windows 8_Mediaelement_Xaudio2 - Fatal编程技术网

Windows 8 在Metro XAudio2和windows Metro上播放声音会引发异常

Windows 8 在Metro XAudio2和windows Metro上播放声音会引发异常,windows-8,mediaelement,xaudio2,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;

我正在尝试在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;
};

我通过了调试器,wfx和SourceVoice结构看起来很好。有人能帮我找出哪里出了问题吗?

错误代码是-2003435519或0x7769FFFF。我在事件查看器中也找不到任何内容。很抱歉,找到了问题。如果通道为1或2,则wfx.Format.wFormatTag需要为WAVE_Format_PCM,无论结构是否定义为WaveFormatTextEnsible。能否将您的解决方案作为答案发布?使用解决方案编辑上述代码。我的意思是:在StackOverflow上,我们尝试让“答案”回答“问题”,它比解析注释更容易阅读。
if( FAILED(hr = XAudio2->CreateSourceVoice( &SourceVoice, (WAVEFORMATEX*)&wfx ) ))
    throw Platform::Exception::CreateException(hr);