Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't play the same song twice #836

Closed
seeseekey opened this issue Sep 6, 2024 · 6 comments
Closed

Can't play the same song twice #836

seeseekey opened this issue Sep 6, 2024 · 6 comments
Labels

Comments

@seeseekey
Copy link

seeseekey commented Sep 6, 2024

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:

  • OS: macOS
  • LITIENGINE version: 0.8.0
  • Java JDK/JRE version: OpenJDK 22
@seeseekey seeseekey added the bug label Sep 6, 2024
Copy link

github-actions bot commented Sep 6, 2024

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

@nightm4re94
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

Implemented and tested, works perfect. Thanks :)

@nightm4re94
Copy link
Member

awesome - happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants