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

Author Topic: (SOLVED)Music not playing even though its loaded.  (Read 129 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
(SOLVED)Music not playing even though its loaded.
« on: April 23, 2024, 12:43:38 am »
I have this strange problem where the music is loaded but nothing happens when the program is run.

This is put before my window.isOpen() loop.
        Music themusic;
        if(!themusic.openFromFile("ASSETS/snow.wav"))
        {
                MessageBox(NULL,"Not found","Error",MB_OK);

        }
        themusic.setVolume(100);
        themusic.play();


The game updates fine.Theres no freezing going on.And I'm sure the audio file works.I have been using audio files in music and suddenly it doesn't make a sound.I'm sure my computer audio works too because sf::sound works. Either i'm doing something wrong here or something in my other code is stopping themusic. I'm certain there is no themusic.stop() anywhere in my code so i'm wondering if its some sort of problem with sfml. I did use 500 sounds. Could this effect sf:Music? I know. Why use 500 sounds? Well its actually for a sound that is played very fast so I need it.
Any ideas where the problem is?
Thanks
« Last Edit: April 24, 2024, 06:08:30 pm by Me-Myself-And-I »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Music not playing even though its loaded.
« Reply #1 on: April 23, 2024, 10:35:19 am »
Can you provide a compilable and minimal example that reproduces the problem?

On its own the code you showed should work.

Make sure you're using the OpenAL DLL that ships with SFML and not your own version or a version that's somewhere in PATH.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Music not playing even though its loaded.
« Reply #2 on: April 24, 2024, 02:55:33 am »
So strange. I cut it down to a bare minimum and I located the problem at the window declaration.

In main.h
#include <SFML\Graphics.hpp>




extern sf::RenderWindow window;
 


In main.cpp
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include "main.h"
#include <windows.h>




               
sf::RenderWindow window(sf::VideoMode(960,540),"Weather Dodger");



int main()
{
       
       
       






        sf::Music themusic;
        if(!themusic.openFromFile("ASSETS/snow.wav"))
        {
                MessageBox(NULL,"Not found","Error",MB_OK);

        }
        themusic.setVolume(100);
        themusic.play();


        while(window.isOpen())
        {

                sf::Event e;
                while(window.pollEvent(e))
                {
                        if(e.type==sf::Event::Closed)
                                window.close();
                }
               
       
               
               
               
               
               
               
               
               
                window.clear();

                window.display();
        }
}



 

With this code the music does not play.


But with window declared locally theres no problem.Any ideas why this problem occurs?

In main.cpp with window local.

#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include "main.h"
#include <windows.h>




               



int main()
{
       
       
       



        sf::RenderWindow window(sf::VideoMode(960,540),"Weather Dodger");



        sf::Music themusic;
        if(!themusic.openFromFile("ASSETS/snow.wav"))
        {
                MessageBox(NULL,"Not found","Error",MB_OK);

        }
        themusic.setVolume(100);
        themusic.play();


        while(window.isOpen())
        {

                sf::Event e;
                while(window.pollEvent(e))
                {
                        if(e.type==sf::Event::Closed)
                                window.close();
                }
               
       
               
               
               
               
               
               
               
               
                window.clear();

                window.display();
        }
}








 

Seems like a window error to me.But why? :P

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Music not playing even though its loaded.
« Reply #3 on: April 24, 2024, 07:55:36 am »
You shouldn't use any SFML resources globally, in general avoid globals as much as possible.
Initialization and destruction order at a global scope aren't guaranteed, so you can run into issues where some expected internal SFML global isn't initialize or already destructed, leading to crashes. As for globals in general, they make it very hard to impossible to track the state and code flow at any given moment.

Why this would have an impact on the music playing or not playing, I don't know, since those are separate modules without any overlaps.  ???
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: Music not playing even though its loaded.
« Reply #4 on: April 24, 2024, 08:56:45 am »
I tried the same code (both global and local window) and music played fine in both cases. Although I'm using SFML 3 so maybe it doesn't have the issue.
I even tried playing the music without a window and it worked. (I replaced the while open loop with a Sleep(10000)).

But I see some bits of the audio system have changed since SFML 2.5 with how the global audio device is handled.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Music not playing even though its loaded.
« Reply #5 on: April 24, 2024, 06:08:16 pm »

Quote
Why this would have an impact on the music playing or not playing, I don't know, since those are separate modules without any overlaps.  ???
I too thought that was very strange.


Quote
You shouldn't use any SFML resources globally, in general avoid globals as much as possible.
Initialization and destruction order at a global scope aren't guaranteed, so you can run into issues where some expected internal SFML global isn't initialize or already destructed, leading to crashes. As for globals in general, they make it very hard to impossible to track the state and code flow at any given moment.

The solution is for me to make a better habit of not declaring SFML elements globally. Thankyou for your help.


Quote
I tried the same code (both global and local window) and music played fine in both cases. Although I'm using SFML 3 so maybe it doesn't have the issue.
I even tried playing the music without a window and it worked. (I replaced the while open loop with a Sleep(10000)).

But I see some bits of the audio system have changed since SFML 2.5 with how the global audio device is handled.

Yes, its likely that this problem was fixed in sfml 3. I use 2.4.0 so it might be more likely for this error to occur in this version when the window is declared globally.