I've been learning how to use AudioStreamPlayer by creating different test projects. This one is a soundtrack player where you can go into another scene and play music. But for some reason it strikes me as poor coding because it looks like there are other better ways to go about it. The whole thing seems inefficient for some reason.
Here's the project folder:
https://workupload.com/file/EnxRmFabZef
Music Room scene:
`extends Control
func _ready() -> void:
pass
func _process(delta: float) -> void:
pass
func _on_play_button_pressed() -> void:
if GlobalAudioStreamPlayer.stream_track1.is_playing() == false:
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track5.stop()
GlobalAudioStreamPlayer.stream_track1.play()
elif GlobalAudioStreamPlayer.stream_track2.is_playing() == false:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track5.stop()
GlobalAudioStreamPlayer.stream_track2.play()
elif GlobalAudioStreamPlayer.stream_track3.is_playing() == false:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track5.stop()
GlobalAudioStreamPlayer.stream_track3.play()
elif GlobalAudioStreamPlayer.stream_track4.is_playing() == false:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track5.stop()
GlobalAudioStreamPlayer.stream_track4.play()
elif GlobalAudioStreamPlayer.stream_track5.is_playing() == false:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track5.play()
pass
func _on_previous_button_pressed() -> void:
if GlobalAudioStreamPlayer.stream_track1.is_playing() == true:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track5.play()
elif GlobalAudioStreamPlayer.stream_track2.is_playing() == true:
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track1.play()
elif GlobalAudioStreamPlayer.stream_track3.is_playing() == true:
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track2.play()
elif GlobalAudioStreamPlayer.stream_track4.is_playing() == true:
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track3.play()
elif GlobalAudioStreamPlayer.stream_track5.is_playing() == true:
GlobalAudioStreamPlayer.stream_track5.stop()
GlobalAudioStreamPlayer.stream_track4.play()
pass
func _on_next_button_pressed() -> void:
if GlobalAudioStreamPlayer.stream_track1.is_playing() == true:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track2.play()
elif GlobalAudioStreamPlayer.stream_track2.is_playing() == true:
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track3.play()
elif GlobalAudioStreamPlayer.stream_track3.is_playing() == true:
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track4.play()
elif GlobalAudioStreamPlayer.stream_track4.is_playing() == true:
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track5.play()
elif GlobalAudioStreamPlayer.stream_track5.is_playing() == true:
GlobalAudioStreamPlayer.stream_track5.stop()
GlobalAudioStreamPlayer.stream_track1.play()
pass
func _on_stop_button_pressed() -> void:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track5.stop()
pass
func _on_shuffle_button_pressed() -> void:
GlobalAudioStreamPlayer.playlist_1.shuffle()
print(GlobalAudioStreamPlayer.playlist_1)
GlobalAudioStreamPlayer.playlist_1[1].stop()
GlobalAudioStreamPlayer.playlist_1[2].stop()
GlobalAudioStreamPlayer.playlist_1[3].stop()
GlobalAudioStreamPlayer.playlist_1[4].stop()
GlobalAudioStreamPlayer.playlist_1[0].play()
pass
func _on_return_button_pressed() -> void:
GlobalAudioStreamPlayer.stream_track1.stop()
GlobalAudioStreamPlayer.stream_track2.stop()
GlobalAudioStreamPlayer.stream_track3.stop()
GlobalAudioStreamPlayer.stream_track4.stop()
GlobalAudioStreamPlayer.stream_track5.stop()
get_tree().change_scene_to_file("res://assets/main_menu.tscn")
`
Audio Stream Player scene:
`extends Node
@onready var stream_track1 = $track1
@onready var stream_track2 = $track2
@onready var stream_track3 = $track3
@onready var stream_track4 = $track4
@onready var stream_track5 = $track5
@onready var stream_MM_bgm = $mm_bgm
@onready var playlist_1: Array = [stream_track1, stream_track2, stream_track3, stream_track4, stream_track5]
@onready var playlist_all: Array = [stream_track1, stream_track2, stream_track3, stream_track4, stream_track5, stream_MM_bgm]
func _ready():
pass
`