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

Author Topic: sf::Music playing a track again  (Read 7389 times)

0 Members and 6 Guests are viewing this topic.

Koschi

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
sf::Music playing a track again
« on: June 27, 2015, 11:06:21 am »
Hi all,

I got a problem with playing a track again.

I got a std::vector<sf::Music*> with 2 tracks I can play every track once, but when i start playing again the first element nothing happens.

My question is how to repeat a played song again, without using the loop-function?
I tryed:
- stop() and then play()
- setPlayingOffset (sf::milisecond(0))
- setLoop(true) play() setLoop(false)

relevant code
void MusicManager::update()
{
    // m_isPlaying is true when manager start playing the first track
    if(!m_isPlaying || m_music[m_currentIndex]->getStatus() == sf::Music::Playing)
        return;

    if(m_currentIndex + 1 < m_music.size())
        ++m_currentIndex;
    else
        m_currentIndex = 0;
   
    m_music[m_currentIndex]->play();
}
 

Any suggestions?

Thnx.

Koschi

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #1 on: June 27, 2015, 11:55:11 am »
Take a look at my Jukebox class. It can do what you want (and more) and is quite straight forward.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Music playing a track again
« Reply #2 on: June 27, 2015, 01:28:32 pm »
Have you tried playing the just as is, without your logic code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Music playing a track again
« Reply #3 on: June 27, 2015, 03:58:45 pm »
I don't understand the need for testing m_isPlaying. Try removing that  :P
(basically, at the moment, it says that if "it's not playing" or "it is playing", you exit the update function)

Also, why are you using pointers to an sf::Music?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Koschi

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #4 on: June 27, 2015, 04:31:19 pm »
Take a look at my Jukebox class. It can do what you want (and more) and is quite straight forward.
Thnx for sharing code, I can't see big difference.
Have you tried playing the just as is, without your logic code?
No, but i debuged my code and he worked well, except play() want start again the music.

I don't understand the need for testing m_isPlaying. Try removing that  :P
(basically, at the moment, it says that if "it's not playing" or "it is playing", you exit the update function)

Also, why are you using pointers to an sf::Music?
When i call MusicManager::play() m_isPlaying is set true, so update method will exit when not starting playing. 'Or' when sf::Music:: is playing update methode should exit. It works!

I use pointer because resourceManager hold the origin.

So is that right, after a track is finished a simple call of play() will start again the track?


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Music playing a track again
« Reply #5 on: June 27, 2015, 05:25:48 pm »
I'm not sure what you're doing, but the following code works fine for me.

#include <SFML/Audio.hpp>
#include <SFML/System.hpp>

int main()
{
        sf::Music music;
        music.openFromFile("music.ogg");
        music.play();

        while(music.getStatus() == sf::Music::Playing)
        {
                sf::sleep(sf::seconds(1.f));
        }

        music.play();

        while(music.getStatus() == sf::Music::Playing)
        {
                sf::sleep(sf::seconds(1.f));
        }
}
 

This works as well:

#include <SFML/Audio.hpp>
#include <SFML/System.hpp>

int main()
{
        sf::Music music;
        music.openFromFile("music.flac");
        music.setLoop(true);
        music.play();

        sf::sleep(sf::seconds(10.f));
}
 
« Last Edit: June 27, 2015, 05:29:34 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Koschi

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #6 on: June 27, 2015, 05:38:59 pm »
try a minimal excemple and it dont work.

#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
 
int main()
{
        sf::Music music;
        music.openFromFile("res/music/neue_musik.ogg");
        music.play();
 
        while(music.getStatus() == sf::Music::Playing)
        {
                sf::sleep(sf::seconds(1.f));
        }
 
        music.play(); // <-- this second play() call dont work for me, i cant hear anything
 
        while(music.getStatus() == sf::Music::Playing)
        {
                sf::sleep(sf::seconds(1.f));
        }
}
 

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #7 on: June 27, 2015, 07:32:08 pm »
OS? SFML version?
SFML / OS X developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #8 on: June 27, 2015, 08:10:38 pm »
What compiler? What compiler version? (adding to Hiura's question)

Koschi

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #9 on: June 27, 2015, 09:26:48 pm »
On my Laptop:
Win XP SP3
SFML 2.3
VC2010 pro

Test it now on my desktop computer and here it works.
on desktop computer:
Win 7
SFML 2.3
VC2010 express

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #10 on: June 27, 2015, 10:01:21 pm »
My gut feeling tells me that XP is so old and deprecated that it's cause of your troubles...
SFML / OS X developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Music playing a track again
« Reply #11 on: June 27, 2015, 10:19:40 pm »
Not at all related to your actual issue, but still, why on earth don't you upgrade to VS2013 (and soon 2015)???

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: sf::Music playing a track again
« Reply #12 on: June 27, 2015, 10:27:58 pm »
XP only supports VS2010, plus he has an old laptop. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: AW: sf::Music playing a track again
« Reply #13 on: June 27, 2015, 10:32:28 pm »
XP only supports VS2010
Not true. We build our software at work with VS2013 and target XP just fine. (Not that I like it, but it works).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Music playing a track again
« Reply #14 on: June 27, 2015, 10:58:07 pm »
Well you're missing the point. Yes you can target XP with VS 2013, but you can not develop with VS 2013 on XP. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything