using new 3.0 Audio system as singleton

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Squatnet

I’m trying to port a project over from 2.1 to 3.0.
I was in previous version using this code (autoload) to play my game music without any interruptions when switching scenes.

extends StreamPlayer


func _ready():
    pass
func playMusic():
    self.set_stream(preload('res://audio/music.ogg'))
    self.set_loop(true)
    self.play()

How would i acheive something similar using the new audio engine in Godot 3?

:bust_in_silhouette: Reply From: Bartosz

Follow tutorial how to use AutoLoad then just create AudioStreamPlayer and add to it music of youre choice. You can also create separate bus just for music if you need it.

extends Node

func _ready():
  var music_file = "res://music.wav"
  var stream = AudioStream.new()
  var music_player = AudioStreamPlayer.new()
  if File.new().file_exists(music_file):
    var music = load(music_file)
    music_player.stream = music
    music_player.play()

    # below are optional steps if you need more control

    var music_bus_id = AudioServer.get_bus_count()
    AudioServer.add_bus()
    AudioServer.set_bus_name(music_bus_id,"music")
    # connects music to master bus
    AudioServer.set_bus_send(music_bus_id,"Master")

    add_child(music_player)
    music_player.bus = "music"