0
\$\begingroup\$

I finished my game programming but I have small problem, please help me I want to finish it completely. I am starting background music when the user goes to the PlayScreen and when the kid in the game dies, the background music stops and starts game over music and move to game over screen, then the user goes to the menu screen and choose the level he wants then go back to the PlayScreen but the back music isn't working again, I didn't know how to solve it although the music is working very well when start the game and when the user win the level, but it is not working when the kid dies. here is the code where I stop the music in the kid class:

public State getState()
    {
        //Gdx.app.log(Float.toString(b2body.getLinearVelocity().x),"hi");
        if ((Hud.getTime()<0)) {
            Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
            Fruits.manager.get("music/fail.mp3", Sound.class).play();
            collectoIsDead=true;
            Filter filter = new Filter();
            filter.maskBits = Fruits.NOTHING_BIT;
            for (Fixture fixture : b2body.getFixtureList())
                fixture.setFilterData(filter);
            b2body.applyLinearImpulse(new Vector2(0, 5f), b2body.getWorldCenter(), true);
            //b2body.applyLinearImpulse(new Vector2(0,-2.5f), b2body.getWorldCenter(), true);
            Hud.setTime();// to prevent the action of applying more times

            return State.DEAD;
        }
        if ( Rockhit()) {
            Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
            Fruits.manager.get("music/fail.mp3", Sound.class).play();
            collectoIsDead=true;
            Filter filter = new Filter();
            filter.maskBits = Fruits.NOTHING_BIT;
            for (Fixture fixture : b2body.getFixtureList())
                fixture.setFilterData(filter);
            b2body.applyLinearImpulse(new Vector2(0, 5f), b2body.getWorldCenter(), true);
            //b2body.applyLinearImpulse(new Vector2(0,-2.5f), b2body.getWorldCenter(), true);
            return State.DEAD;
        }
        if((Hud.getTime()==0&& Hud.getScore()>=(level*30)+50)|| (Hud.getScore()>=(level*30)+50)) {
            Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
            Fruits.manager.get("music/cheering.mp3", Sound.class).play();
            return State.SUCCESS;
        }
        if (b2body.getLinearVelocity().x!=0)
            return State.RUNNING;
        else
            return State.STANDING;
    }

And here is the code where I start the music in the constructor of the PlayScreen:

public PlayScreen(Fruits game, float level)
    {
        this.level=level;
        this.game=game;
        gamecam=new OrthographicCamera();

        gameport=new FitViewport(Fruits.V_WIDTH/Fruits.PPM,Fruits.V_HIEGT/Fruits.PPM,gamecam);

        hud=new Hud(game.batch,level);
        atlas=new TextureAtlas("mypack.pack");
        mapLoader=new TmxMapLoader();

            map=mapLoader.load("world10.tmx");

        renderer=new OrthogonalTiledMapRenderer(map,1/Fruits.PPM);
        gamecam.position.set(gameport.getWorldWidth()/2,gameport.getWorldHeight()/2f,0);
        globalcounter=100;
        world=new World(new Vector2(0,-10),true);
        b2dr=new Box2DDebugRenderer();
        new B2WorldCreator(this);
        player=new Collector(world,this,level);
        world.setContactListener(new WorldContactListener());
        controller=new Controller(game.batch);

        music=Fruits.manager.get("music/Backmusic.ogg", Music.class);
        music.setLooping(true);
        music.play();
    }
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

The best way to load in music and sound effects resources is through LibGDX's audio methods:

Music music = Gdx.audio.newMusic(Gdx.files.internal(path_to_music_file));
Sound sound = Gdx.audio.newSound(Gdx.files.internal(path_to_sound_file));

To play the music:

music.setVolume(1f);
music.play();

To play the sound effect:

long id = sound.play();
sound.setVolume(id, 1f);

It is best practice to isolate your resources from the rest of the model. You may or may not have done this yet - I can't judge from your code - but it is best to create a resource handler, which stores resources (textures, music, sound) locally (e.g. in HashMaps dedicated to each type of resource), and which enables you to load them all from one source later. An example of such a resource handler may be:

public class ResourceHandler {

    //HashMaps for storing resources
    private HashMap<String, Texture> textures;
    private HashMap<String, Music> music;
    private HashMap<String, Sound> sounds;

    public ResourceHandler() {
        textures = new HashMap<String, Texture>();
        music = new HashMap<String, Music>();
        sounds = new HashMap<String, Sound>();
    }

    public void loadTexture(String path, String key) {
        Texture texture = new Texture(Gdx.files.internal(path));
        textures.put(key, texture);
    }

    public void loadMusic(String path, String key) {
        Music musicfile = Gdx.audio.newMusic(Gdx.files.internal(path));
        music.put(key, musicfile);
    }

    public void loadSound(String path, String key) {
        Sound soundfile = Gdx.audio.newSound(Gdx.files.internal(path));
        sounds.put(key, soundfile);
    }

    public Texture getTexture(String key) {
        return textures.get(key);
    }

    public Music getMusic(String key) {
        return music.get(key);
    }

    public Sound getSound(String key) {
        return sounds.get(key);
    }

}

If doing this, there should be no problems with playing sounds or music, provided that the file you're loading in is actually functional.

\$\endgroup\$
0
0
\$\begingroup\$

If you want repetitive music, use:

music.setLooping(true);

after

music.play();
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .