如果您查看 Music.cpp 中的代码
bool Music::OnGetData(SoundStream::Chunk& data)
{
Lock lock(myMutex);
// Fill the chunk parameters
data.Samples = &mySamples[0];
data.NbSamples = myFile->Read(&mySamples[0], mySamples.size());
// Check if we have reached the end of the audio file
return data.NbSamples == mySamples.size();
}
您会看到它在文件末尾时将返回 false。
所以你想做的是子类 sf::Music。例如
class MyMusic : public sf::Music
{
bool OnGetData(SoundStream::Chunk& data)
{
bool running = sf::Music::OnGetData(data);
if(!running)
OnMusicEnd();
return running;
}
public:
void OnMusicEnd()
{
// ...
}
};