Skip to content

Commit e3a4bf9

Browse files
committed
Make audio playback toggle present during session
1 parent 99a4a7a commit e3a4bf9

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

firebaseai/FirebaseAIExample/Features/Live/Screens/LiveScreen.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ struct LiveScreen: View {
5050
)
5151

5252
#if targetEnvironment(simulator)
53-
if viewModel.state == .idle {
54-
AudioOutputToggle(isEnabled: $viewModel.isAudioOutputEnabled)
55-
}
53+
AudioOutputToggle(isEnabled: $viewModel.isAudioOutputEnabled, onChange: {
54+
Task {
55+
await viewModel.onAudioPlaybackChanged()
56+
}
57+
})
5658
#endif
5759
}
5860
.padding()

firebaseai/FirebaseAIExample/Features/Live/ViewModels/LiveViewModel.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class LiveViewModel: ObservableObject {
9696

9797
guard await requestRecordPermission() else {
9898
logger.warning("The user denied us permission to record the microphone.")
99+
isAudioOutputEnabled = false
99100
return
100101
}
101102

@@ -118,6 +119,16 @@ class LiveViewModel: ObservableObject {
118119
}
119120
}
120121

122+
func onAudioPlaybackChanged() async {
123+
if isAudioOutputEnabled {
124+
guard await requestRecordPermission() else {
125+
logger.warning("The user denied us permission to record the microphone.")
126+
isAudioOutputEnabled = false
127+
return
128+
}
129+
}
130+
}
131+
121132
/// Disconnects the model.
122133
///
123134
/// Will stop any pending playback, and the recording of the mic.

firebaseai/FirebaseAIExample/Features/Live/Views/AudioOutputToggle.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import SwiftUI
1616

1717
struct AudioOutputToggle: View {
1818
@Binding var isEnabled: Bool
19+
var onChange: (Bool) -> Void = { _ in }
1920

2021
var body: some View {
2122
VStack(alignment: .leading, spacing: 5) {
22-
Toggle("Audio Output", isOn: $isEnabled)
23-
.toggleStyle(.switch)
23+
Toggle("Audio Output", isOn: $isEnabled).onChange(of: isEnabled) { _, new in
24+
onChange(new)
25+
}
2426

2527
Text("""
2628
Audio output works best on physical devices. Enable this to test playback in the \

0 commit comments

Comments
 (0)