Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How can I know if the song ended or not?  (Read 6233 times)

0 Members and 1 Guest are viewing this topic.

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
How can I know if the song ended or not?
« on: June 08, 2014, 01:15:58 am »
Let's say I have a Music object and I load a certain song to it:

Code: [Select]
sf::Music song;
song.openFromFile("path.wav");
song.play();

I want to know if a song ended playing or not. If it did I want to load another song. I tried doing something like this:

Code: [Select]
if (song.getStatus() == sf::Music::Status::Stopped) {
   song.openFromFile("anothersong.wav");
   song.play();
}

But it doesn't work and the problem seems to be with the condition. This one doesn't say me if the song ended. When the song ends status should be set to stopped, right? I thought so and I thought that my code would work, but it doesn't. I tried several things (I thought about checking song duration, and counting time) and I checked if getStatus() method returns the right value (when I press "S" the song stops playing and its status is set to Stopped, when I press "P" the song starts playing and its status is set to Playing - that works fine, but seems that when the song ends status is still set to Playing). How can I check if song ended playing?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How can I know if the song ended or not?
« Reply #1 on: June 08, 2014, 01:23:27 am »
My Jukebox class that I've posted on the wiki does just this (continue with next song when the first is done). Perhaps taking a look at it will help you : https://github.com/SFML/SFML/wiki/Source%3A-Jukebox

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: How can I know if the song ended or not?
« Reply #2 on: June 08, 2014, 02:44:07 am »
Thanks, dude! All that I had to do was change Music::Stopped to SoundSource::Stopped. Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How can I know if the song ended or not?
« Reply #3 on: June 08, 2014, 09:14:20 am »
Quote
Thanks, dude! All that I had to do was change Music::Stopped to SoundSource::Stopped. Thanks!
Hum? This is the exact same value, sf::Musics inherits sf::SoundSource (and thus its Stopped constant).
Laurent Gomila - SFML developer

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: How can I know if the song ended or not?
« Reply #4 on: June 08, 2014, 03:01:21 pm »
I know, but honestly I have no idea why SoundSource::Stopped is working and Music::Status::Stopped isn't. Perhaps it's something with my IDE and the version of SFML I am using. I'm working on Visual Studio 2013 and I currently have SFML 2.1 for Visual 2010.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How can I know if the song ended or not?
« Reply #5 on: June 08, 2014, 03:08:24 pm »
Music::Status::Stopped
You mentioned Music::Stopped before. Music::Status::Stopped is not allowed until C++11.

I'm working on Visual Studio 2013 and I currently have SFML 2.1 for Visual 2010.
This won't work, you have to recompile SFML for Visual Studio 2013.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything