我正在尝试制作简单的声音缓冲区并使用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;
    }

最佳答案

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

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

09-09 18:43