115 lines
3.5 KiB
GDScript
115 lines
3.5 KiB
GDScript
extends Control
|
|
|
|
|
|
@onready var music_player:AudioStreamPlayer = $music
|
|
var official_music:Dictionary = {}
|
|
var current_area:String = "extra"
|
|
var current_music:String = ""
|
|
|
|
func _ready():
|
|
var directory:Directory = Directory.new()
|
|
var files = []
|
|
get_tree().paused = false
|
|
Common.msg_debug("Load MusicManager")
|
|
if not directory.dir_exists( "res://music/" ):
|
|
Common.msg_debug("No Music")
|
|
return
|
|
directory.open( "res://music/" )
|
|
for dir in directory.get_directories():
|
|
var dir2:Directory = Directory.new()
|
|
var fullpath: String = "res://music/" + dir
|
|
dir2.open( fullpath )
|
|
Common.msg_debug("Dir: " + str(dir))
|
|
for file in dir2.get_files():
|
|
if file == "":
|
|
break
|
|
elif not file.begins_with(".") and not dir2.current_is_dir() and (file.ends_with( ".ogg" ) or file.ends_with( ".mp3" )):
|
|
if ! official_music.has(dir):
|
|
official_music[dir] = []
|
|
official_music[dir].append(fullpath + "/" + file)
|
|
files.append(file)
|
|
Common.msg_debug("File: " + str(file))
|
|
Common.msg_debug("List: " + str(official_music))
|
|
# music_player.connect("finished", self._on_stream_finished.bind())
|
|
# music_player.connect("finished", _on_stream_finished.bind())
|
|
#music_player.finished.connect(_on_stream_finished.bind())
|
|
music_player.finished.connect(self._on_stream_finished.bind())
|
|
#music_player.loop = false
|
|
if official_music.has(current_area):
|
|
var first = official_music[current_area][0]
|
|
Common.msg_debug("Play music: " + first)
|
|
play_music(first)
|
|
|
|
|
|
func load_external_music(filepath):
|
|
var stream = null
|
|
Common.msg_debug("load_external_music")
|
|
var file = File.new()
|
|
file.open(filepath, file.READ)
|
|
var ext = filepath.split(".")[-1].to_lower()
|
|
var buffer = file.get_buffer(file.get_len())
|
|
Common.msg_debug(ext)
|
|
match ext:
|
|
"ogg":
|
|
stream = AudioStreamOGGVorbis.new()
|
|
"mp3":
|
|
stream = AudioStreamMP3.new()
|
|
#"wav":
|
|
# # We need decode message to get format (Stero/size bit/ ...)
|
|
# stream = AudioStreamSample.new()
|
|
# stream.format = AudioStreamSample.FORMAT_16_BITS
|
|
# #stream.stereo = true
|
|
_:
|
|
Common.msg_error("Impossible to identify type of file (file:" + filepath + ", ext:" + ext + ")")
|
|
return
|
|
stream.data = buffer
|
|
Common.msg_debug("load_external_music : End")
|
|
return stream
|
|
|
|
|
|
func load_music(filepath: String):
|
|
if filepath.left(5) == "res:/":
|
|
return load( filepath )
|
|
return load_external_music(filepath)
|
|
|
|
|
|
func _on_stream_finished():
|
|
Common.msg_debug("_on_stream_finished")
|
|
music_player.stop()
|
|
|
|
|
|
func play_music(filename: String):
|
|
if current_music == filename:
|
|
return
|
|
#music_player.stream = load_music( filename )
|
|
music_player.set_stream(load_music( filename ))
|
|
music_player.play()
|
|
music_player.get_stream().set_loop(false)
|
|
Common.msg_debug("Signal:" + str(music_player.get_signal_list()))
|
|
Common.msg_debug("Signal:" + str(music_player.get_stream().get_signal_list()))
|
|
Common.msg_debug("Signal connecte:" + str(music_player.finished.get_connections()))
|
|
# music_player.connect("finished", _on_stream_finished.bind())
|
|
current_music = filename
|
|
# music_play = true
|
|
# for child in $window_box/scroll_box/musics_box.get_children():
|
|
# child.set_play()
|
|
# if Config.mute:
|
|
# child.set_disabled()
|
|
# elif child.get_music() == filename or not Config.playermusic :
|
|
# child.set_play()
|
|
# else:
|
|
# child.set_unplay()
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
# if ! music_player.get_stream().is_playing():
|
|
# Common.msg_debug("Not playing")
|
|
# else:
|
|
# Common.msg_debug("Playing")
|
|
pass
|
|
|
|
|
|
func _on_music_finished():
|
|
Common.msg_debug("_on_music_finished")
|
|
music_player.stop()
|