我的程序运行不正常。我需要使用 boost::asio 播放从网络获取的声音。那个声音是对的,因为如果我把它交给文件,我就有声音文件。但是在扬声器中,我会听到重复的咔嗒声。那是我的代码:
while (!ExitKey)
{
boost::system::error_code error;
size_t len = VoiceSocket->read_some(boost::asio::buffer(buf), error);
if (len==0)
{
continue;
}
file.write(buf.data(), 882);
if (firstit++>=3)
{
alSourceUnqueueBuffers(alSource, 1, &alUnqueueBuf);
}
if (iteration <=3)
{
alBufferData(alSampleSet[iteration++], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
}
else
{
iteration = 0;
alBufferData(alSampleSet[iteration++], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
}
//alSourceQueueBuffers(alSource, 3, alSampleSet);
alSourcei(alSource, AL_BUFFER, alSampleSet[0]);
alSourcei(alSource, AL_BUFFER, alSampleSet[1]);
alSourcei(alSource, AL_BUFFER, alSampleSet[2]);
alSourceQueueBuffers(alSource, 1, alSampleSet);
//
//alSourcei(alSource, AL_LOOPING, alSampleSet);
ALenum alErr0 = AL_NO_ERROR;
ALenum alErr1 = AL_INVALID_NAME;
ALenum alErr2 = AL_INVALID_ENUM;
ALenum alErr3 = AL_INVALID_VALUE;
ALenum alErr4 = AL_INVALID_OPERATION;
ALenum alErr5 = AL_OUT_OF_MEMORY;
ALenum alErr = alGetError();
//alGetSourcei(alSource, AL_SOURCE_STATE, &alErr);
alSourcePlay(alSource);
alErr = alGetError();
//
}
请帮我。我需要用这段代码做什么才能让它工作。我不明白如何使用这个缓冲区:(