gurkenlabs / litiengine

LITIENGINE 🕹 The pure 2D java game engine.
https://litiengine.com/
MIT License
733 stars 93 forks source link

Can't play the same song twice #836

Closed seeseekey closed 1 month ago

seeseekey commented 1 month ago

Describe the bug If i want play a song twice (play song, stop it, play it again), the second attempt doesn't work. It need to play another song first to play the first song again.

To Reproduce

public static void main(String[] args) throws InterruptedException {

        LOG.info("Init engine");
        Game.init(args);

        // Song (1) play
        LOG.info("Play song (1)");
        Game.audio().playMusic(new SinglePlayTrack("audio/music/endless-nights.ogg"));

        // Wait
        Thread.sleep(3000);

        // Stop music
        Game.audio().stopMusic();

        // Song (2) doesn't play
        LOG.info("Play song (2)");
        Game.audio().playMusic(new SinglePlayTrack("audio/music/endless-nights.ogg"));
    }

Expected behavior Song should be started, then stopped, then again started.

Your System:

github-actions[bot] commented 1 month ago

Thank you for your reporting your first LITIENGINE issue! We are looking forward to your further contributions.

nightm4re94 commented 1 month ago

Thanks for the report! Why would you want to initialize a MusicPlayback that already exists in the first place? I suggest instead to pause the MusicPlayback directly:

// considering there is already a Sound Resource with the music you want to play, use the String parameter for `playMusic`.
Game.audio().playMusic("endless-nights.ogg");

// access the MusicPlayback
MusicPlayback mp = Game.audio().getMusic();

// pause and resume
mp.pausePlayback();
mp.resumePlayback();

// stop and restart
mp.cancel();
mp.run();
seeseekey commented 1 month ago

It's a "slide-based" approach. There are some different slides defined in a JSON file. If one slide is loaded a new screen for the slide is started and a music resource would be played. In some cases two slides with the same music comes successively.

Sure, technically I can check if the music file is the same and only restart it, but from the outer view if I start a music via Game.audio().playMusic I want that the engine plays the music instead of being silent.

nightm4re94 commented 1 month ago

In that case, this is not a bug. You can simply use Game.audio().playMusic(Track track, boolean restart) to check if the new track is equal to the one currently playing and restart it.

seeseekey commented 1 month ago

Implemented and tested, works perfect. Thanks :)

nightm4re94 commented 1 month ago

awesome - happy coding!