IXAudio2-> CreateSoundVoice返回错误

时间:2019-11-10 09:39:04

标签: c++ winapi xaudio2

我正在尝试制作简单的声音缓冲区并使用XAudio 2播放它。

首先我已经初始化IXAudio2

IXAudio2* XAudio = 0;
int XAudioInitFailed = XAudio2Create(&XAudio, 0, XAUDIO2_DEFAULT_PROCESSOR);

//check for failure

IXAudio2MasteringVoice* MasterVoice = 0;
XAudio->CreateMasteringVoice(&MasterVoice);

接下来,我填充我的数据。

// Mono 8-bit 44.1kHz PCM format
WAVEFORMATEX WaveFormat = {};
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nChannels = 1;
WaveFormat.nSamplesPerSec = 44100;
WaveFormat.wBitsPerSample = 8;
WaveFormat.nBlockAlign = WaveFormat.nChannels * WaveFormat.wBitsPerSample / 8;
WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
WaveFormat.cbSize = sizeof(WAVEFORMATEX);

// Populating my SoundBuffer
// Creating XAudio sound buffer struct

IXAudio2SourceVoice* SourceVoice = 0;
int code = XAudio->CreateSourceVoice(&SourceVoice, &WaveFormat);
SourceVoice->SubmitSourceBuffer(&XBuffer); //this line triggers exceptions
SourceVoice->Start(0);

当我运行代码时,在行SourceVoice->SubmitSourceBuffer(&XBuffer);上触发了异常,代码为0x88960001,根据Microsoft Windows API页面,这意味着XAUDIO2_E_INVALID_CALL

帮我弄清楚问题可能出在哪里。

更新:完整代码:

WinAudio = {};
    IXAudio2* XAudio = 0;
    int XAudioInitFailed = XAudio2Create(&XAudio, 0, XAUDIO2_DEFAULT_PROCESSOR);
    if(!XAudioInitFailed) {

        //NOTE: creating the mastering voice
        IXAudio2MasteringVoice* MasterVoice = 0;
        XAudio->CreateMasteringVoice(&MasterVoice);

        //NOTE: initializing PlayBuffer
        // Mono 8-bit 44.1kHz PCM format
        WAVEFORMATEX WaveFormat = {};
        WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
        WaveFormat.nChannels = 1;
        WaveFormat.nSamplesPerSec = 44100;
        WaveFormat.wBitsPerSample = 8;
        WaveFormat.nBlockAlign = WaveFormat.nChannels * WaveFormat.wBitsPerSample / 8;
        WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
        WaveFormat.cbSize = sizeof(WAVEFORMATEX);

        // NOTE: initializing SoundBuffer
        int SampleFrequency = 256;
        int Samples = WaveFormat.nSamplesPerSec;
        int AudioBytes = Samples * WaveFormat.nBlockAlign;
        i1* SoundBuffer = (i1*)malloc(AudioBytes);
        i1* WritePointer = SoundBuffer;
        for(int Sample = 0;
            Sample < Samples;
            Sample ++) {
            i1 value = ((Sample / SampleFrequency)%2)*10;

            *WritePointer++ = value;
            //*WritePointer++ = value;
        }

        // NOTE: Creating Sound
        XAUDIO2_BUFFER XBuffer = {};
        XBuffer.Flags = XAUDIO2_END_OF_STREAM;
        XBuffer.AudioBytes = AudioBytes;
        XBuffer.pAudioData = (const BYTE*)SoundBuffer;
        // NOTE: The following parameters are
        // measured in samples
        XBuffer.PlayBegin = 0;
        XBuffer.PlayLength = 0; 
        XBuffer.LoopBegin = 0;
        XBuffer.LoopLength = 0;
        XBuffer.LoopCount = XAUDIO2_LOOP_INFINITE;

        IXAudio2SourceVoice* SourceVoice = 0;
        int code = XAudio->CreateSourceVoice(&SourceVoice, &WaveFormat);
        SourceVoice->SubmitSourceBuffer(&XBuffer);
        SourceVoice->Start(0);
    } else {
        // TODO: Diagnose
        InvalidCodePath;
    }

1 个答案:

答案 0 :(得分:1)

您必须检查CreateMasterVoice的错误,如果失败,CreateSourceVoice也将失败。 错误XAUDIO2_E_INVALID_CALL使我认为CreateMasterVoice不成功。

CreateMasterVoice中的错误将为您提供有关此问题的更多提示。 例如,您可能忘记了在创建XAudio2之前调用CoInitialize的情况,在这种情况下CreateMasterVoice失败,并出现CO_E_NOTINITIALIZED