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

Author Topic: Program loads .WAV but does not play it back an/or I cant hear it!  (Read 8459 times)

0 Members and 3 Guests are viewing this topic.

TheRavenBlue

  • Newbie
  • *
  • Posts: 8
    • View Profile
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!
« Last Edit: December 16, 2015, 10:15:35 pm by TheRavenBlue »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #1 on: December 16, 2015, 10:36:14 pm »
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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

TheRavenBlue

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #2 on: December 16, 2015, 11:16:39 pm »
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.
« Last Edit: December 16, 2015, 11:30:23 pm by TheRavenBlue »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #3 on: December 17, 2015, 12:09:29 am »
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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #4 on: December 17, 2015, 01:05:46 am »
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.

TheRavenBlue

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #5 on: December 17, 2015, 08:26:55 am »
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;
}
« Last Edit: December 17, 2015, 03:42:25 pm by TheRavenBlue »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #6 on: December 18, 2015, 09:34:38 pm »
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"?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

TheRavenBlue

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #7 on: December 18, 2015, 09:53:22 pm »
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?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #8 on: December 19, 2015, 12:10:27 am »
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. 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();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

TheRavenBlue

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #9 on: December 19, 2015, 05:19:22 pm »
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. 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?
« Last Edit: December 19, 2015, 06:28:45 pm by TheRavenBlue »

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #10 on: December 19, 2015, 05:26:49 pm »
Rewrite that if statement. You can't have if a == b || c, you need ( a == b || a == c ) , with the brackets in this case.

TheRavenBlue

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #11 on: December 19, 2015, 06:27:07 pm »
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!

Satus

  • Guest
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #12 on: December 19, 2015, 06:52:19 pm »
 if (Bool.EnableMusic = true &&

I think you want ==

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #13 on: December 19, 2015, 07:06:26 pm »
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();
    }
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Program loads .WAV but does not play it back an/or I cant hear it!
« Reply #14 on: December 19, 2015, 07:27:18 pm »
Perhaps my Jukebox class will be useful to you - even if not directly, then perhaps as a source of inspiration.