Skip to content

Commit 581b7a6

Browse files
committed
Guarantee that avatar is available when SpatialAudioPlayer is created
1 parent b33e7c2 commit 581b7a6

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

Sources/AlloReality/SpatialAudioPlayer.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,36 @@ public class SpatialAudioPlayer
1919
let mapper: RealityViewMapper
2020
let client: AlloUserClient
2121
let content: RealityViewContentProtocol
22+
let avatarId: EntityID
2223
fileprivate var state: [MediaStreamId: SpatialAudioPlaybackState] = [:]
2324
var cancellables: Set<AnyCancellable> = []
2425

2526
// TODO: Maybe take which entity to attach Listeners to instead of assuming avatar?
26-
27-
public init(mapper: RealityViewMapper, client: AlloUserClient, content: RealityViewContentProtocol)
27+
/// Construct a SpatialAudioPlayer which uses `mapper` to create audio related components and `client` to react to network events. Note: announce must have completed and avatar exist before instantiating this class.
28+
public init(mapper: RealityViewMapper, client: AlloUserClient, content: RealityViewContentProtocol, avatarId: EntityID)
2829
{
2930
self.mapper = mapper
3031
self.client = client
3132
self.content = content
33+
self.avatarId = avatarId
3234
start()
3335
}
3436

37+
// Guaranteed to be called _after_ avatar and initial state is loaded
3538
func start()
3639
{
37-
// TODO: await avatarId before even starting this function, so we can assume we have it.
38-
// TODO: Or maybe even in viewModel, await avatar before setting MOST of that stuff up
39-
4040
// 0. Setup audio listener
41-
client.$avatarId.sink { avatarId in
42-
guard let avatarId else { return }
43-
let guient = self.mapper.guiForEid(avatarId)!
44-
self.useAsListener(guient)
45-
}.store(in: &cancellables)
41+
let avatar = client.avatar!
42+
let guient = self.mapper.guiForEid(avatarId)!
43+
self.useAsListener(guient)
4644

4745
// 1. Setup listeners to get incoming tracks. Just ask to get everything (except our own audio) forwarded.
4846
var streamIds = Set<String>()
4947
func updateListener()
5048
{
5149
Task { @MainActor in
5250
print("SpatialAudioPlayer Updating listener to forward \(streamIds)")
53-
try! await self.client.avatar!.components.set(LiveMediaListener(mediaIds: streamIds))
51+
try! await avatar.components.set(LiveMediaListener(mediaIds: streamIds))
5452
}
5553
}
5654
client.placeState.observers[LiveMedia.self].added.sink { eid, liveMedia in

0 commit comments

Comments
 (0)