Skip to content

Commit 72b88b1

Browse files
authored
Enable preconnect (#18)
1 parent 8ced7de commit 72b88b1

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

VoiceAssistant/Connect/ControlBar.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,18 @@ struct ControlBar: View {
106106
let participantName = "user-\(Int.random(in: 1000 ... 9999))"
107107

108108
do {
109-
// Fetch connection details from token service
110-
if let connectionDetails = try await tokenService.fetchConnectionDetails(
111-
roomName: roomName,
112-
participantName: participantName
113-
) {
114-
// Connect to the room and enable the microphone
115-
try await room.connect(
116-
url: connectionDetails.serverUrl, token: connectionDetails.participantToken,
117-
connectOptions: ConnectOptions(enableMicrophone: true)
118-
)
119-
isConnecting = false
120-
} else {
121-
print("Failed to fetch connection details")
109+
try await room.withPreConnectAudio {
110+
Task { @MainActor in isConnecting = false }
111+
// Fetch connection details from token service
112+
guard let connectionDetails = try await tokenService.fetchConnectionDetails(roomName: roomName, participantName: participantName) else {
113+
print("Failed to fetch connection details")
114+
return
115+
}
116+
// Connect to the room
117+
try await room.connect(url: connectionDetails.serverUrl, token: connectionDetails.participantToken, connectOptions: .init(enableMicrophone: true))
122118
}
123119
} catch {
124120
print("Connection error: \(error)")
125-
isConnecting = false
126121
}
127122
}
128123
}

VoiceAssistant/ContentView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ struct ContentView: View {
1616
let room = Room()
1717
_room = StateObject(wrappedValue: room)
1818
_chatViewModel = State(initialValue: ChatViewModel(room: room, messageReceivers: TranscriptionStreamReceiver(room: room)))
19+
20+
do {
21+
try AudioManager.shared.setRecordingAlwaysPreparedMode(true)
22+
} catch {
23+
print("Failed to prepare recording")
24+
}
1925
}
2026

2127
var body: some View {

0 commit comments

Comments
 (0)