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

Author Topic: [Resolved] Moving to next or previous song  (Read 4115 times)

0 Members and 1 Guest are viewing this topic.

Psychohyena

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Resolved] Moving to next or previous song
« on: March 28, 2010, 12:06:46 pm »
I'm writing a program which as a side thing will have a jukebox feature.

The problem I have run into is that when moving to the next song or a previous song, the program gets the filename from the jukebox class and then opens the file.

minimal Jukebox code:
Code: [Select]

class Jukebox
{
    public  :
                //Load next song.
                std::string LoadSong()
                {
                    std::stringstream Filename;
                    Filename << MusicLoca.c_str() << MusicList[(lastSong+1)].c_str();
                    lastSong += 1;
                    std::cout<<Filename.str() << lastSong;
                    return Filename.str();
                }
private :
    std::string MusicLoca;
    std::string MusicList[19];
    int lastSong;
}


Access from main program:
Code: [Select]
Music.OpenFromFile(myJukebox.LoadSong());
        Music.Play();//Works
        Sleep(1000);
        Music.Stop();
        Music.OpenFromFile(myJukebox.LoadSong());
        Music.Play();//Doesn't work.


Now I know the Filename is being returned because of the std::cout.
I believe the issue is that I'm not using the sf::Music class correctly. If anyone can help that would be great.

Cheers,
Psychohyena

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved] Moving to next or previous song
« Reply #1 on: March 28, 2010, 02:22:15 pm »
I remember fixing a similar bug in 1.6.

If you don't want to get it from the SVN, or wait for the public release, I think that using a new sf::Music instance instead of reusing the previous one is enough to make it work.
Laurent Gomila - SFML developer

Psychohyena

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Resolved] Moving to next or previous song
« Reply #2 on: March 28, 2010, 10:31:43 pm »
Thanks for the suggestion Laurent, I didn't have any success though. :( I know where the problem is coming from now though if that helps.

It has something to do with sf::Music::Stop(). If I load a song in and then use stop and then play it doesn't work, but if I use sf::Music::Pause() and then change the song it does work.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved] Moving to next or previous song
« Reply #3 on: March 28, 2010, 11:06:35 pm »
Ok, so you should try 1.6 ;)
Laurent Gomila - SFML developer

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[Resolved] Moving to next or previous song
« Reply #4 on: June 03, 2010, 02:17:25 am »
I'm getting a very similar issue to this with SFML 1.6

Whenever my character dies, I play a death song. This plays correctly. Whenever I attempt to switch back to the main song however, it incorrectly plays the death song again.

Code: [Select]

if(deathTimer.GetElapsedTime() < 7 && immortal.GetElapsedTime() > 7)
    {
        if(currentSong != "Death.ogg")
        {
            MusicManager::getInstance()->getResource(currentSong)->Stop();
            currentSong = "Death.ogg";
            MusicManager::getInstance()->getResource(currentSong)->Play();
        }
    }
    else if(currentSong != "Relief.ogg")
    {
        MusicManager::getInstance()->getResource(currentSong)->Stop();
        currentSong = "Relief.ogg";
        MusicManager::getInstance()->getResource(currentSong)->Play();
    }


When it calls attempts to restart Relief.ogg, it instead plays Death.ogg

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved] Moving to next or previous song
« Reply #5 on: June 03, 2010, 08:18:25 am »
Could you please provide a minimal example that reproduces this problem, so that I can quickly test and debug it on my computer?
Laurent Gomila - SFML developer

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[Resolved] Moving to next or previous song
« Reply #6 on: June 04, 2010, 04:15:10 am »
I'm currently putting together a small example to reproduce this, but one thing I noticed is that it only happens when using OpenFromMemory. If I use OpenFromFile it works fine.

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[Resolved] Moving to next or previous song
« Reply #7 on: June 04, 2010, 04:31:04 am »
Code: [Select]

int main()
{
    sf::Clock clock;

MusicManager::getInstance()->getResource("Death.ogg")->Play();
while(clock.GetElapsedTime() < 7);
clock.Reset();

MusicManager::getInstance()->getResource("Death.ogg")->Stop();
MusicManager::getInstance()->getResource("Relief.ogg")->Play();
while(clock.GetElapsedTime() < 7);
clock.Reset();

MusicManager::getInstance()->getResource("Relief.ogg")->Stop();
MusicManager::getInstance()->getResource("Death.ogg")->Play();
while(clock.GetElapsedTime() < 7);
clock.Reset();

MusicManager::getInstance()->getResource("Death.ogg")->Stop();
MusicManager::getInstance()->getResource("Relief.ogg")->Play();
while(clock.GetElapsedTime() < 7);
clock.Reset();

    return EXIT_SUCCESS;
}


Death.ogg doesn't play the second time.

Code: [Select]

MusicManager* MusicManager::musicManager;

sf::Music* MusicManager::load(const std::string& stringID)
{
    sf::Music* music = new sf::Music();

    char* buffer = resourceLoader.GetFile(stringID);

    if(!music->OpenFromMemory(buffer, resourceLoader.GetFileSize(stringID)))
    {
        #ifdef DEBUG
            std::cout<< "MusicManager failed to load: " << stringID << std::endl;
        #endif
    }

    music->SetVolume(25);

    return music;
}


The above is my music manager class which is inheriting from:
http://www.sfml-dev.org/wiki/en/sources/resource_manager

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved] Moving to next or previous song
« Reply #8 on: June 04, 2010, 08:20:54 am »
Thanks, I'll test it as soon as possible.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved] Moving to next or previous song
« Reply #9 on: June 04, 2010, 09:05:32 pm »
It works for me.

Here is my complete test code:
Code: [Select]

#include <SFML/Audio.hpp>
#include <iostream>
#include <string>
#include <map>
 
// [the ResourceManager class from the wiki]

class MusicManager : public ResourceManager<sf::Music>
{
public:

    static MusicManager* getInstance()
    {
        static MusicManager instance;
        return &instance;
    }

private:

    virtual sf::Music* load(const std::string& stringID)
    {
        sf::Music* music = new sf::Music();
        if(!music->OpenFromFile(stringID))
        {
            #ifdef DEBUG
                std::cout<< "MusicManager failed to load: " << stringID << std::endl;
            #endif
        }

        music->SetVolume(25);

        return music;
    }
};


int main()
{
    std::string m1 = "datas/sound/footsteps.wav";
    std::string m2 = "datas/sound/lepidoptera.ogg";

    sf::Clock clock;

    MusicManager::getInstance()->getResource(m1)->Play();
    while(clock.GetElapsedTime() < 7);
    clock.Reset();

    MusicManager::getInstance()->getResource(m1)->Stop();
    MusicManager::getInstance()->getResource(m2)->Play();
    while(clock.GetElapsedTime() < 7);
    clock.Reset();

    MusicManager::getInstance()->getResource(m2)->Stop();
    MusicManager::getInstance()->getResource(m1)->Play();
    while(clock.GetElapsedTime() < 7);
    clock.Reset();

    MusicManager::getInstance()->getResource(m1)->Stop();
    MusicManager::getInstance()->getResource(m2)->Play();
    while(clock.GetElapsedTime() < 7);
    clock.Reset();

    return EXIT_SUCCESS;
}

The musics are those from the SFML SDK.

The only difference is that I didn't use your ResourceLoader class. You should test my code, and if it works then show me your resource loader.
Laurent Gomila - SFML developer

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[Resolved] Moving to next or previous song
« Reply #10 on: June 05, 2010, 02:21:02 am »
The issue only happens when using OpenFromMemory.

 

anything