我写了这段代码:
void Sound::GoChat()
{
device = alcOpenDevice(NULL);
//
ofstream file;
file.open("TESTSPEAKER", std::ios_base::binary);
context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
// alGetError();
char *alBuffer;
ALenum alFormatBuffer;
ALsizei alFreqBuffer;
long alBufferLen;
ALboolean alLoop;
unsigned int alSource;
unsigned int alSampleSet[3];
boost::array <char, 882> buf;
int NumberOfSampleSet = 0;
alGenSources(1, &alSource);
alGenBuffers(3, alSampleSet);
//
alBufferData(alSampleSet[0], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
alBufferData(alSampleSet[1], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
alBufferData(alSampleSet[2], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
alSourceQueueBuffers(alSource, 3, alSampleSet);
//
while (!ExitKey)
{
alSourceUnqueueBuffers(alSource, 3, alSampleSet);
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);
alBufferData(alSampleSet[0], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
alBufferData(alSampleSet[1], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
alBufferData(alSampleSet[2], AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
alSourcei(alSource, AL_BUFFER, alSampleSet[0]);
alSourcei(alSource, AL_BUFFER, alSampleSet[1]);
alSourcei(alSource, AL_BUFFER, alSampleSet[2]);
alSourceQueueBuffers(alSource, 3, alSampleSet);
//
//alSourcei(alSource, AL_LOOPING, alSampleSet);
alSourcePlay(alSource);
alErr = alGetError();
//
}
VoiceSocket->close();
file.close();
}
但这也行不通(