-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMusicAPI.gd
39 lines (31 loc) · 918 Bytes
/
MusicAPI.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Music API
# The music is played and controlled here
extends Node
signal stream_ready(stream)
signal stop_stream()
var playlist: Array = []
var playing_music: bool = false
var wait_for_track: bool = true # Checks if track playing currently is finished
func add_song(song_path: String):
playlist.append(song_path)
if !playing_music:
_play_music()
func _play_music():
playing_music = true
for song in playlist:
stream_music(song)
if wait_for_track:
yield(get_node("/root/MusicViz"), "track_finished") # Wait for signal
playlist.remove(playlist.find(song))
playing_music = false
func stream_music(song_path):
var file = File.new()
file.open(song_path, File.READ)
var buffer = file.get_buffer(file.get_len())
var stream = AudioStreamMP3.new()
stream.data = buffer
emit_signal("stream_ready", stream)
func clear_queue():
playlist.clear()
playing_music = false
emit_signal("stop_stream")