XAudio2-只播放部分音频
XAudio2 - playing only a part of the audio
我正在尝试使用xaudio2播放mp3/wma。我设法使用Media Foundation Source Reader对象来进行解码。我的问题是,它没有播放完整的音频;我只能播放一部分音频。
我想做的是,从IMFSourceReader获得下一个样本,并将其作为sourcevoice的下一个缓冲区提交。重复此操作直到从IMFSourceReader读取所有数据。
while (true)
{
DWORD dwFlags = 0;
// Read the next sample.
hr = pReader->ReadSample(
(DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM,
0, NULL, &dwFlags, NULL, &pSample );
if (dwFlags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED)
{
printf("Type change - not supported by WAVE file format.n");
break;
}
if (dwFlags & MF_SOURCE_READERF_ENDOFSTREAM)
{
printf("End of input file.n");
break;
}
if (pSample == NULL)
{
printf("No samplen");
continue;
}
// Get a pointer to the audio data in the sample.
hr = pSample->ConvertToContiguousBuffer(&pBuffer);
if (FAILED(hr)) { break; }
hr = pBuffer->Lock(&pAudioData, NULL, &cbBuffer);
if (FAILED(hr)) { break; }
// Make sure not to exceed the specified maximum size.
if (cbMaxAudioData - cbAudioData < cbBuffer)
{
cbBuffer = cbMaxAudioData - cbAudioData;
}
// Write this data to the output file.
hr = WriteToFile(hFile, pAudioData, cbBuffer);
int audioBufferLength = cbBuffer;
if (FAILED(hr)) { break; }
SubmitBuffer(pAudioData, audioBufferLength);
// Unlock the buffer.
hr = pBuffer->Unlock();
pAudioData = NULL;
if (FAILED(hr)) { break; }
// Update running total of audio data.
cbAudioData += cbBuffer;
if (cbAudioData >= cbMaxAudioData)
{
break;
}
SafeRelease(&pSample);
SafeRelease(&pBuffer);
}
void AudioDecoder::SubmitBuffer(byte *pAudioData, int audioBufferLength)
{
byte * pAudioBuffer = new byte[audioBufferLength];
CopyMemory(pAudioBuffer, pAudioData, audioBufferLength);
if (pAudioBuffer != nullptr)
{
// Create an XAUDIO2_BUFFER for submitting audio data
XAUDIO2_BUFFER buffer = {0};
buffer.AudioBytes = audioBufferLength;
buffer.pAudioData = pAudioBuffer;
buffer.pContext = pAudioBuffer;
HRESULT hresult = m_pSourceVoice->SubmitSourceBuffer(&buffer);
}
}
在此之后,我将调用m_pSourceVoice->Start()。这将启动音频,但不会播放完整的音频。我还需要添加其他内容吗?
这个循环看起来不考虑在提交更多缓冲区之前是否已经完成任何缓冲区,因此可能会达到XAUDIO2_MAX_QUEUED_buffers的限制。你能在while循环中创建一个计数器来查看有多少缓冲区提交给源语音吗?
如果你达到了限制,你可以在完全解码文件之前开始播放,并通过源语音回调提交额外的缓冲区。http://msdn.microsoft.com/en-us/library/windows/desktop/ee415769(v=vs.85).aspx
相关文章:
- C++新手,想知道如何使用VS code 2019播放音频文件
- 如何以编程方式将音频从任何录制设备路由到任何播放设备
- 大声音频无法播放
- Qt C++ 创建方形音频音调波.播放并保存它
- C++从字节数组实时播放音频
- 无法在带有 gst-launch-1.0 的 Qemu 的 armv7 上播放 wav 音频
- 如何使用Microsoft::D irectX::AudioVideoPlayback 来播放音频
- 如何将 cdparanoia 读取的 CD 音频数据移交给 ALSA 播放器?
- 如何在Raspberry Pi上使用C /libao/alsa通过USB声卡播放音频
- QT 创建器:程序启动时音量滑块仍播放音频
- 如何播放AMR音频,QMediaPlayer不支持
- 使用音频设备播放WebRTC C++应用程序的音频流
- 是否可以使用带有random_shuffle的矢量随机播放音频文件
- 在 openGL 中播放音频而不冻结绘制循环
- 将超能力高级音频播放器同步到绝对位置
- Qt - 如何设置音频播放从缓冲区的开头开始
- Qt和音频播放
- 如何使用waveOutWrite()使音频播放流畅
- 如何获得Windows音频播放的当前采样率
- Qt声子音频播放器中的循环问题