C++ 播放流声音开放
我需要用OpenAL播放流音。这是我的代码,但不起作用。我需要做什么C++ 播放流声音开放,c++,audio,boost,openal,C++,Audio,Boost,Openal,我需要用OpenAL播放流音。这是我的代码,但不起作用。我需要做什么 device = alcOpenDevice(NULL); // ofstream file; file.open("TESTSPEAKER", std::ios_base::binary); context = alcCreateContext(device, NULL); alcMakeContextCurrent(context); // alGetError();
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;
boost::array <char, 882> buf;
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);
alGenSources(1, &alSource);
alGenBuffers(1, &alSampleSet);
alBufferData(alSource, AL_FORMAT_MONO16, buf.data(), buf.size(), 44100);
alSourcei(alSource, AL_BUFFER, alSampleSet);
//
//alSourcei(alSource, AL_LOOPING, alSampleSet);
alSourcePlay(alSource);
//alSourcePlay(alSource);
}
VoiceSocket->close();
file.close();
device=alcOpenDevice(空);
//
流文件;
打开(“TESTSPEAKER”,std::ios\u base::binary);
context=alcreatecontext(设备,空);
alcMakeContextCurrent(上下文);
//阿尔及利亚恐怖();
字符*缓冲区;
阿仑木;
ALsizei-Alfreq缓冲区;
长alBufferLen;
布尔alLoop;
无符号int-alSource;
无符号int-alsampleleset;
boost::阵列buf;
而(!ExitKey)
{
boost::system::error\u code error;
size\u t len=VoiceSocket->read\u some(boost::asio::buffer(buf),错误);
如果(len==0)
{
继续;
}
file.write(buf.data(),882);
阿尔及利亚资源(1,&alSource);
alGenBuffers(1和alSampleSet);
alBufferData(alSource,AL_FORMAT_MONO16,buf.data(),buf.size(),44100);
alSourcei(alSource、AL_缓冲区、alSampleSet);
//
//alSourcei(alSource、AL_循环、alSampleSet);
alSourcePlay(alSource);
//alSourcePlay(alSource);
}
VoiceSocket->close();
file.close();
如果我看到我的文件(“TESTSPEAKER”),我会观察我的声音。所以我在网络上正确地传递了它。但是我如何用openal播放这个声音呢?如果你是流媒体,那么你应该使用
alSourceQueueBuffers(alSource, 1, &alSampleSet);
而不是将其设置为静态缓冲区,如
alSourcei(alSource, AL_BUFFER, alSampleSet);
并发出命令
alSourcePlay(alSource)代码>只有一次,当第一个缓冲区排队时。我需要每10毫秒播放882字节的声音(MONO16)。这个声音在“buf”变量中。