Continue playing music after changing scene

Shirted-VanesShirted-Vanes Posts: 13Member

Hello!
I'm using AudioStreamPlayer, my music is working well. But when I change scene by
get_tree().change_scene(path)
music plays from the beginning again.
I tried to do this:

I have global script "GLOBAL", there is float variable "timecode" with default value "0.0".
However, it doesn't work.
Am I doing something wrong?


Tags :

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,113
    Accepted Answer

    I think the pause is because the audio runs on a separate thread than the game logic, so the micro pause is the time it takes to communicate to the audio to continue playing. At least, that is what I gather from this page on syncing gameplay with audio on the latest Godot documentation.

    In the jam version of Dreams of Fire, I got around this issue by fading the music. I lowered the volume down to zero when changing, and then in the new scene I would slowly increase the volume back to the default, which worked fairly well at the time.

    There might be a way to tell the audio to instantly start playing, but I have no idea what it would be, unfortunately. One way you could maybe get around the issue is by instancing and managing an AudioStreamPlayer node in the global singleton directly, though that will have it's own complexities.

    Unfortunately, I really don't know. Hopefully someone who knows more will chime in with a potential solution.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,113Admin

    Is the timecode variable being reset anywhere? Maybe it is getting reset before it has a chance to change the value in the AudioStreamPlayer node. Maybe print timecode before self.play(GLOBAL.timecode) and see what the value is?

    Looking at the code posted, I don't see anything really that could be causing an issue. The only other thing that might be contributing to the problem is loading the music using load, which may allow _process to be called before _ready is finished, but I don't think this is the case.

  • Shirted-VanesShirted-Vanes Posts: 13Member

    Okay, I tried some magic, it works pretty good.
    But is it normal that it's changing so harshly? Like there's micro-pause when scene is being changed.
    Can I fix it somehow? Smooth transition?

  • TwistedTwiglegTwistedTwigleg Posts: 2,113Admin
    Accepted Answer

    I think the pause is because the audio runs on a separate thread than the game logic, so the micro pause is the time it takes to communicate to the audio to continue playing. At least, that is what I gather from this page on syncing gameplay with audio on the latest Godot documentation.

    In the jam version of Dreams of Fire, I got around this issue by fading the music. I lowered the volume down to zero when changing, and then in the new scene I would slowly increase the volume back to the default, which worked fairly well at the time.

    There might be a way to tell the audio to instantly start playing, but I have no idea what it would be, unfortunately. One way you could maybe get around the issue is by instancing and managing an AudioStreamPlayer node in the global singleton directly, though that will have it's own complexities.

    Unfortunately, I really don't know. Hopefully someone who knows more will chime in with a potential solution.

  • AlexandrosKapAlexandrosKap Posts: 22Member
    edited July 2019

    You could create a GDScript that extends AudioStreamPlayer and use it as a singleton. In the script set the song and the values you want to have.

    This is the script I use in my game.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file