Skip to content

Commit 00479cd

Browse files
authored
Merge pull request #4 from FreekDS/fix-issue2-issue3
Add shortcut methods + fix issue with multiple AudioEventPlayers having a single AudioStreamPlayer
2 parents f8262a5 + ed178b5 commit 00479cd

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

addons/AudioEvents/scripts/AudioStreamPlayerEvents.gd

+66-6
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,58 @@ signal soundEvent(soundName: String, id: int, playedAt: float)
1616

1717
var lastId = -1
1818

19+
#region Public API
20+
## Reset the AudioEventPlayer. Makes sure that all events will be played
21+
## when starting again.
22+
func reset():
23+
lastId = -1
24+
25+
26+
func play():
27+
if not _precondition("Cannot call play()"):
28+
return
29+
30+
# Resume if it was paused
31+
if audioStreamPlayer.stream_paused:
32+
audioStreamPlayer.stream_paused = false
33+
return
34+
35+
# Else reset and start again
36+
reset()
37+
audioStreamPlayer.stream = audioEventStream.audio
38+
audioStreamPlayer.play()
39+
40+
func pause():
41+
if not _precondition("Cannot call pause()"):
42+
return
43+
44+
if audioStreamPlayer.playing:
45+
audioStreamPlayer.stream_paused = true
46+
47+
func stop():
48+
if not _precondition("Cannot call stop()"):
49+
return
50+
reset()
51+
audioStreamPlayer.stop()
52+
53+
#endregion
54+
55+
#region Godot callbacks
1956
func _ready():
20-
var p = get_parent()
21-
if p is AudioStreamPlayer:
22-
audioStreamPlayer = p
57+
if audioStreamPlayer == null:
58+
var p = get_parent()
59+
if p is AudioStreamPlayer:
60+
audioStreamPlayer = p
2361
if audioStreamPlayer == null:
2462
push_error("Please assign AudioStreamPlayer, or make sure the parent is one!")
2563
assert(false)
26-
64+
65+
audioStreamPlayer.finished.connect(reset)
66+
2767
if audioEventStream:
2868
audioStreamPlayer.stream = audioEventStream.audio
29-
if audioStreamPlayer.autoplay:
69+
if audioStreamPlayer.autoplay and not audioStreamPlayer.playing:
3070
audioStreamPlayer.play()
31-
3271

3372
func _process(_delta):
3473
if not audioStreamPlayer or not audioEventStream:
@@ -37,6 +76,12 @@ func _process(_delta):
3776
if !audioStreamPlayer.playing:
3877
return
3978

79+
# Make sure to only call events from this AudioEventPlayer
80+
# in case multiple AudioEventStreams are using a single AudioStreamPlayer
81+
# see issue #3
82+
if audioStreamPlayer.stream != audioEventStream.audio:
83+
return
84+
4085
var pos = audioStreamPlayer.get_playback_position()
4186
var index = -1
4287
for t_i in range(len(audioEventStream.eventTimes)):
@@ -49,3 +94,18 @@ func _process(_delta):
4994
if index != -1 and lastId != index:
5095
soundEvent.emit(audioEventStream.name, index, pos)
5196
lastId = index # Avoid doubles
97+
98+
#endregion
99+
100+
#region Private API
101+
func _precondition(err: String) -> bool:
102+
if audioStreamPlayer == null:
103+
push_error(err + ": ", "AudioStreamPlayer is not assigned")
104+
return false
105+
if audioEventStream == null:
106+
push_error(err + ": ", "No AudioEventStream resource attached!")
107+
return false
108+
return true
109+
110+
#endregion
111+

example/example.tscn

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
[sub_resource type="Resource" id="Resource_2ro53"]
99
script = ExtResource("3_rdl2a")
10-
audio = ExtResource("2_c08da")
11-
eventTimes = Array[float]([3.02, 6.96, 13.01])
1210
name = "Audio"
11+
audio = ExtResource("2_c08da")
12+
eventTimes = Array[float]([0.5, 6.96, 13.01])
1313

1414
[node name="Example" type="Node"]
1515

0 commit comments

Comments
 (0)