I am trying to play music using C++11 threads. After initializing OpenAL, I have the following code within my Music::Play()
method:
alSourcePlay(source);
ALint source_state;
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
while (source_state == AL_PLAYING) {
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
alGetSourcef(source, AL_SEC_OFFSET, &song_time);
}
Since this while loop is blocking, I have to run it in a separate thread from my main program logic. I tried adding the following to main()
:
Music music;
std::thread tmusic(&Music::Play, &music);
...and it all worked fine. I then decided to move this functionality to a method in my main program class:
Program::PlayMusic() {
Music music;
std::thread tmusic(&Music::Play, &music);
}
I figured that this was not working because the thread object was immediately going out of scope after Program::PlayMusic()
ended, and the thread was destroyed. I tried detaching the thread from the thread object using tmusic.detach()
, and that seemed to work okay, but the music only plays some of the time. It seems to be related to the size of the track. My original song was a ~350MB WAV file that I was loading using:
buffer = alutCreateBufferFromFile("music.wav");
trimming the file down to ~4MB seems to help, but still it only plays some of the time. Any help would be greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire