Search code examples
javalibgdx

Adding music into LibGDX isn't working. Get an error saying that newMusic isn't a method Has anyone else had this problem lately?


intro = new Audio.newMusic(Gdx.files.internal("core/assets/OpenSong.mp3"));

In the javadocs this is how it says to do it

https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/badlogic/gdx/audio/Music.html

https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/badlogic/gdx/Audio.html#newMusic-com.badlogic.gdx.files.FileHandle-

Here: https://www.gamefromscratch.com/post/2013/11/19/LibGDX-Tutorial-8-Audio.aspx they used a lowercase audio instead of uppercase but when changing it to lower case that didn't work either.

It's as if audio/Audio just don't have that method.


Solution

  • Your code should import the class com.badlogic.gdx.Gdx and then you can use

    intro = Gfx.audio.newMusic(Gdx.files.internal("core/assets/OpenSong.mp3"));
    

    Alternatively you can write

    // where all the import statements live
    import static com.badlogic.gdx.Gdx.audio;
    
    //  where you try to load the music
    intro = audio.newMusic(Gdx.files.internal("core/assets/OpenSong.mp3"));
    

    Your code tries to create a new Audio instance (this will not work, since Audio is just an interface - you would need to create an implementation for it).

    Just replacing new Audio with audio will also not work (unless you statically import com.badlogic.gdx.Gdx.audio, as in the second example) - audio alone would mean a field or local variable audio.