SFML community forums
Help => Audio => Topic started by: Psychohyena 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:
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:
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
-
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.
-
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.
-
Ok, so you should try 1.6 ;)
-
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.
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
-
Could you please provide a minimal example that reproduces this problem, so that I can quickly test and debug it on my computer?
-
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.
-
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.
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
-
Thanks, I'll test it as soon as possible.
-
It works for me.
Here is my complete test code:
#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.
-
The issue only happens when using OpenFromMemory.