我正在编写一个可以加载和播放WAV文件的程序,我使用XAudio2库,我开始在msdn.microsoft.com的帮助下编写它,现在我几乎完全复制了来自msdn的代码,它仍然无法正常工作。我不知道问题出在哪里。当我尝试使用CreateSourceVoice播放声音时,我有XAUDIO2_E_INVALID_CALL错误。 我很感激你的帮助。 他是我的代码:
我的临时主:
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR * strFileName = _TEXT("Chimes.wav");
//Creating a instance od XAufio2 Engine
IXAudio2* pXAudio2;
IXAudio2MasteringVoice* pMasteringVoice;
IXAudio2SourceVoice* pSourceVoice;
HRESULT hr;
if ( FAILED( hr = XAudio2Create( &pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) ){
cout << "XAudio2Create Failed!\n";
}
WAVEFORMATEXTENSIBLE wfx = { 0 };
XAUDIO2_BUFFER buffer = { 0 };
//Open the audio file with CreateFile
HANDLE hFile = CreateFile(
strFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL
);
DWORD dwChunkPosition = 0;
DWORD dwChunkSize;
if (INVALID_HANDLE_VALUE == hFile)
cout << "INVALID HANDLE VALUE!\n";
//return HRESULT_FROM_WIN32(GetLastError());
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_BEGIN))
{
cout << "INVALID SET FILE POINTER!\n";
//return HRESULT_FROM_WIN32(GetLastError());
}
cout << "Locating a 'RIFF' and whole data size\n";
FindChunk(hFile, fourccRIFF, dwChunkSize, dwChunkPosition);
cout << "Checking a filetype\n";
DWORD filetype;
ReadChunkData(hFile, &filetype, sizeof(DWORD), dwChunkPosition);
if (filetype != fourccWAVE)
cout << "This isn't WAVE file!\n";
else cout << "WAVE format!\n";
cout << "Locating a fmt and filling out WAVEFORMATEXTENSIBLE structure\n";
FindChunk(hFile, fourccFMT, dwChunkSize, dwChunkPosition);
ReadChunkData(hFile, &wfx, dwChunkSize, dwChunkPosition);
cout << "Locating a DATA\n";
FindChunk(hFile, fourccDATA, dwChunkSize, dwChunkPosition);
cout << "Reading a sound\n";
BYTE * pDataBuffer = new BYTE[dwChunkSize];
ReadChunkData(hFile, pDataBuffer, dwChunkSize, dwChunkPosition);
buffer.AudioBytes = dwChunkSize;
buffer.pAudioData = pDataBuffer;
buffer.Flags = XAUDIO2_END_OF_STREAM;
//Plaing preloaded sound;
if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, (WAVEFORMATEX*)&wfx)))
cout << "Playing sound Failed!\n";
system("PAUSE");
return 0;
}
在WAV文件中搜索Chunks的功能:
HRESULT FindChunk(HANDLE hFile, DWORD fourcc, DWORD & dwChunkSize, DWORD & dwChunkDataPosition)
{
HRESULT hr = S_OK;
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_BEGIN))
return HRESULT_FROM_WIN32(GetLastError());
DWORD dwChunkType;
DWORD dwChunkDataSize;
DWORD dwRIFFDataSize = 0;
DWORD dwFileType;
DWORD bytesRead = 0;
DWORD dwOffset = 0;
while (hr == S_OK)
{
DWORD dwRead;
if (0 == ReadFile(hFile, &dwChunkType, sizeof(DWORD), &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
if (0 == ReadFile(hFile, &dwChunkDataSize, sizeof(DWORD), &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
switch (dwChunkType)
{
case fourccRIFF:
dwRIFFDataSize = dwChunkDataSize;
dwChunkDataSize = 4;
if (0 == ReadFile(hFile, &dwFileType, sizeof(DWORD), &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
break;
default:
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, dwChunkDataSize, NULL, FILE_CURRENT))
return HRESULT_FROM_WIN32(GetLastError());
}
dwOffset += sizeof(DWORD)* 2;
if (dwChunkType == fourcc)
{
dwChunkSize = dwChunkDataSize;
dwChunkDataPosition = dwOffset;
return S_OK;
}
dwOffset += dwChunkDataSize;
if (bytesRead >= dwRIFFDataSize) return S_FALSE;
}
return S_OK;
}
HRESULT ReadChunkData(HANDLE hFile, void * buffer, DWORD buffersize, DWORD bufferoffset)
{
HRESULT hr = S_OK;
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, bufferoffset, NULL, FILE_BEGIN))
return HRESULT_FROM_WIN32(GetLastError());
DWORD dwRead;
if (0 == ReadFile(hFile, buffer, buffersize, &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
答案 0 :(得分:1)
您在哪里实例化母带语音(pMasteringVoice)? XAudio2图表需要Source Voice - &gt;掌握语音 - &gt; XAudio2。
在XAudio2Create调用后添加以下内容
IXAudio2MasteringVoice* pMasteringVoice = NULL;
if ( FAILED(hr = pXAudio2->CreateMasteringVoice( &pMasteringVoice ) ) )
return hr;
http://msdn.microsoft.com/en-us/library/windows/desktop/ee415779(v=vs.85).aspx