播放流声音OpenAL

时间:2013-02-14 11:39:09

标签: c++ audio boost openal

我需要使用OpenAL播放流声音。这是我的代码,但它不起作用。我需要做什么?

    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();

如果我看到我的文件(“TESTSPEAKER”),我会看到我的声音。所以我正确地传递了网络。但我怎么能用openal播放这个声音?

1 个答案:

答案 0 :(得分:0)

如果您正在播放,则应使用

alSourceQueueBuffers(alSource, 1, &alSampleSet);

而不是将其设置为静态缓冲区,如

alSourcei(alSource, AL_BUFFER, alSampleSet);

并在第一个缓冲区排队时发出命令alSourcePlay(alSource);