The general question is how to play music correctly in c++? I've read music should be played in another task. But playing from channel seems to be out of threads. AudioPlayer's destructor is called right after starting playback so channel is freed immediately and I can't hear any sound. How to deal with this problem? Using Bass audio library
void AudioPlayer::play()
{
endOfStream = false;
BASS_ChannelStop(channel);
if (!(channel = BASS_StreamCreateFile(false, filename.c_str(), 0, 0, BASS_SAMPLE_LOOP)) && !(channel = BASS_MusicLoad(false, filename.c_str(), 0, 0, BASS_MUSIC_RAMP | BASS_MUSIC_STOPBACK, 1)))
std::cout << "Can't play file";
BASS_ChannelSetSync(channel, BASS_SYNC_END, 0, &streamEndCallback, 0);
BASS_ChannelPlay(channel, true); //what's going on here? is it another thread already?
is_playing = true;
}
//main
AudioPlayer player(file_path);
std::thread th(&AudioPlayer::play, player); //th's destructor is called immediately, channel and resources are freed
th.join();
while(true) ; //do some processing in main thread
Aucun commentaire:
Enregistrer un commentaire