This repository was archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSimpleAudioPlayer.gd
More file actions
51 lines (41 loc) · 1.52 KB
/
SimpleAudioPlayer.gd
File metadata and controls
51 lines (41 loc) · 1.52 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
extends Spatial
# The audio player node.
var audio_node = null
# A variable to track whether or not we should loop
var should_loop = false
# The globals autoload script
var globals = null
func _ready():
# Get the audio player node, connect the finished signal, and assure it's not playing anything.
audio_node = $Audio_Stream_Player
audio_node.connect("finished", self, "sound_finished")
audio_node.stop()
# Get the globals script
globals = get_node("/root/Globals")
func play_sound(audio_stream, position=null):
# Based on the passed in sound, set the audio stream and then play it.
# If we do not have an sound stream with that name, then simply destroy ourselves.
#
# This is not included in the tutorial, but is here because you have to provide your
# own audio. To make the project work, we add this check.
if audio_stream == null:
print ("No audio stream passed, cannot play sound")
globals.created_audio.remove(globals.created_audio.find(self))
queue_free()
return
# Set the audio stream to the passed in audio stream
audio_node.stream = audio_stream
# If you are using a AudioPlayer3D, then uncomment these lines to set the position.
# if position != null:
# audio_node.global_transform.origin = position
# Play the sound from the beginning
audio_node.play(0.0)
func sound_finished():
if should_loop:
# Start playing again, at the beginning
audio_node.play(0.0)
else:
# Destroy/Free this sound.
globals.created_audio.remove(globals.created_audio.find(self))
audio_node.stop()
queue_free()