-
I'm creating a small game and I wanted my program to play music in the main meny this is how it looks like now:
void MainMenyMusic::PlayMusic()
{
sf::Music music;
music.setVolume(50);
if (music.openFromFile("test.wav") && !music.Playing)
{
music.play();
}
if (!music.openFromFile("test.wav"))
{
exit(0);
}
}
I know that it is not the best but I wanted to test it out I have also done exactly like the tutorial said about playing music but it did not work either.
What is wrong with this?
I'm also using DirectX to draw it all and using SFML to do some back end stuff as networking and sound!
Thanks!
-
if (music.openFromFile("test.wav") && !music.Playing)
{
music.play();
This opens the file and, if successful, starts to play it.
if (!music.openFromFile("test.wav"))
{
exit(0);
}
This - immediately following the previous code - opens the file again (so any previously playing file will be stopped) and, if unsuccessful, closes the program.
-
if (music.openFromFile("test.wav") && !music.Playing)
{
music.play();
This opens the file and, if successful, starts to play it.
if (!music.openFromFile("test.wav"))
{
exit(0);
}
This - immediately following the previous code - opens the file again (so any previously playing file will be stopped) and, if unsuccessful, closes the program.
Ah, okay thank you!
I edited my code so it looks like this now:
void MainMenyMusic::PlayMusic()
{
sf::Music music;
music.setVolume(100);
if (!music.openFromFile("test.wav"))
{
exit(0);
}
music.play();
}
And the program starts but there is still no music.
-
You are creating an sf::Music instance locally inside that method, opening the file, starting it to play, and then the instance gets destroyed as the method ends since it goes out of scope.
-
To add additional detail, music.play() is not a blocking call. It will start playing the music but it won't wait for it to finish playing. Your program will continue on while the music is playing. In this case, as Hapax mentioned, you are then immediately returning from your function, thus destroying your music object and stopping the music from playing. So, it seems like the music is never playing, but what is probably actually happening is that it is starting and then stopping so fast that you never hear it.
-
You are creating an sf::Music instance locally inside that method, opening the file, starting it to play, and then the instance gets destroyed as the method ends since it goes out of scope.
Thanks!
I got it working.
How I use it:
Music->playMenyMusic("Data//Music//MainMeny//MenySound.wav", 25, true);
My function:
int MainMenyMusic::playMenyMusic(string FileLocation, float Volume, bool Loop)
{
pMusic.setVolume(Volume);
pMusic.setLoop(Loop);
if (!pMusic.openFromFile(FileLocation))
{
MessageBox(NULL, "Missing files please re-download the game!", "Error", MB_OK);
if (IDOK)
{
exit(0);
}
}
pMusic.play();
return 0;
}
-
The important thing here is that the actual sf::Music object is created outside of this function as is in existence for the entire time that the music is required so are you now creating "pMusic" elsewhere and keeping it alive?
Why is it called "pMusic"? What's wrong with "music"?
-
The important thing here is that the actual sf::Music object is created outside of this function as is in existence for the entire time that the music is required so are you now creating "pMusic" elsewhere and keeping it alive?
Why is it called "pMusic"? What's wrong with "music"?
There is nothing wrong with "music" I just named it to something random to test it out!
But I'm having another problem, I want to toggle between pMusic.pause();
and pMusic.play();
this is how it looks like right now:
int MainMenyMusic::playMenyMusic(string FileLocation, float Volume, bool Loop)
{
pMusic.setVolume(Volume);
pMusic.setLoop(Loop);
if (!pMusic.openFromFile(FileLocation))
{
MessageBox(NULL, "Missing files please re-download the game!", "Error", MB_OK);
if (IDOK)
{
exit(0);
}
}
if (Bool.EnableMusic == true)
{
pMusic.play();
}
else if (Bool.EnableMusic == false)
{
pMusic.stop();
}
return 0;
}
It plays the music but it does not pause it when EnableMusic == false
any ideas?
-
it does not pause it when EnableMusic == false
pMusic.stop();
For your information, an sf::Music object can supply you with information such as its current playing status (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1SoundStream.php#a75f722e7edcfa9952ff0c643966c6858). It can tell you if the music is currently playing, stopped or paused.
e.g.
if (music.getStatus() == sf::Music::Paused)
music.play();
else
music.pause();
-
it does not pause it when EnableMusic == false
pMusic.stop();
For your information, an sf::Music object can supply you with information such as its current playing status (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1SoundStream.php#a75f722e7edcfa9952ff0c643966c6858). It can tell you if the music is currently playing, stopped or paused.
e.g.
if (music.getStatus() == sf::Music::Paused)
music.play();
else
music.pause();
Hm, okay thanks this could come in handy.
But it does not pause / stop the music it continues to play no matter what.
if (Bool.EnableMusic = true && (pMusic.getStatus() == sf::Music::Stopped || pMusic.getStatus() == sf::Music::Paused))
pMusic.play();
else if (Bool.EnableMusic == false && pMusic.getStatus() == sf::Music::Playing)
pMusic.pause();
Does the: pMusic.setLoop(Loop);
have something to do with it not pausing or stopping?
-
Rewrite that if statement. You can't have if a == b || c, you need ( a == b || a == c ) , with the brackets in this case.
-
Rewrite that if statement. You can't have if a == b || c, you need ( a == b || a == c ) , with the brackets in this case.
Thanks for pointing that out I did not see that!
-
if (Bool.EnableMusic = true &&
I think you want ==
-
There's no reason to explicitly compare to bool literals, the values themselves are already true or false. Furthermore, explicitly writing the negated condition in the else branch is unnecessary and confusing.
if (Bool.EnableMusic == true)
{
pMusic.play();
}
else if (Bool.EnableMusic == false)
{
pMusic.stop();
}
should be (not taking into account Hapax' comment about querying the status):
if (Bool.EnableMusic)
{
pMusic.play();
}
else
{
pMusic.stop();
}
-
Perhaps my Jukebox class (https://github.com/SFML/SFML/wiki/Source:-Jukebox)will be useful to you - even if not directly, then perhaps as a source of inspiration.
-
There's no reason to explicitly compare to bool literals, the values themselves are already true or false. Furthermore, explicitly writing the negated condition in the else branch is unnecessary and confusing.
if (Bool.EnableMusic)
{
pMusic.play();
}
else
{
pMusic.stop();
}
I tried to make that already and it did not work either but I will take a look into Jesper's Jukebox source and Ill return with an answer if I can get it to work or not!