检查音乐是否结束,OnStopped?

发布于 2024-10-28 01:09:47 字数 310 浏览 0 评论 0原文

是否可以让 SFML 1.6 自己处理音乐结束?目前我有这个:

//in music.cpp
music.Play()

//in main.cpp
//on every frame check for end of music
if(music.getStatus() == Sound::Stopped)
    loadNextMusicFile();

SFML 中是否有一种方法可以直接说“播放直到音乐停止,然后加载下一个”,而无需自己实现?或者至少有一种更“优雅”的方式来注意到音乐何时停止(例如 OnStopped 事件)?

Is it possible to make SFML 1.6 handle end of music by itself? Currently I have this:

//in music.cpp
music.Play()

//in main.cpp
//on every frame check for end of music
if(music.getStatus() == Sound::Stopped)
    loadNextMusicFile();

Isn't there a way in SFML to just say, "Play until music stopped, then load the next," without implementing this yourself? Or at least a more "elegant" way of noticing when the music stopped (like an OnStopped event)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2024-11-04 01:09:48

如果您查看 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()
   {
       // ...
   }

};

If you look at the code from 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();
}

You see that it will return false when its at the end of the file.

So what you want to do is subclass sf::Music. e.g.

class MyMusic : public sf::Music
{
   bool OnGetData(SoundStream::Chunk& data)
   {
       bool running = sf::Music::OnGetData(data);
       if(!running)
          OnMusicEnd();
       return running;
   }
public:
   void OnMusicEnd()
   {
       // ...
   }

};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文