Merge pull request #992 from pipecat-ai/live-updates-to-selected-and-available-mics

In the iOS SimpleChatbot demo, wire up live updates to the selected m…
This commit is contained in:
kompfner
2025-01-15 15:00:14 -05:00
committed by GitHub
2 changed files with 39 additions and 21 deletions

View File

@@ -24,6 +24,16 @@ class CallContainerModel: ObservableObject {
var rtviClientIOS: RTVIClient? var rtviClientIOS: RTVIClient?
@Published var selectedMic: MediaDeviceId? = nil {
didSet {
guard let selectedMic else { return } // don't store nil
var settings = SettingsManager.getSettings()
settings.selectedMic = selectedMic.id
SettingsManager.updateSettings(settings: settings)
}
}
@Published var availableMics: [MediaDeviceInfo] = []
init() { init() {
// Changing the log level // Changing the log level
PipecatClientIOS.setLogLevel(.warn) PipecatClientIOS.setLogLevel(.warn)
@@ -52,15 +62,19 @@ class CallContainerModel: ObservableObject {
) )
self.rtviClientIOS?.delegate = self self.rtviClientIOS?.delegate = self
self.rtviClientIOS?.start() { result in self.rtviClientIOS?.start() { result in
if case .failure(let error) = result { switch result {
case .failure(let error):
self.showError(message: error.localizedDescription) self.showError(message: error.localizedDescription)
self.rtviClientIOS = nil self.rtviClientIOS = nil
case .success():
// Apply initial mic preference
if let selectedMic = currentSettings.selectedMic {
self.selectMic(MediaDeviceId(id: selectedMic))
}
// Populate available devices list
self.availableMics = self.rtviClientIOS?.getAllMics() ?? []
} }
} }
// Selecting the mic based on the preferences
if let selectedMic = currentSettings.selectedMic {
self.rtviClientIOS?.updateMic(micId: MediaDeviceId(id:selectedMic), completion: nil)
}
self.saveCredentials(backendURL: baseUrl) self.saveCredentials(backendURL: baseUrl)
} }
@@ -114,6 +128,11 @@ class CallContainerModel: ObservableObject {
SettingsManager.updateSettings(settings: currentSettings) SettingsManager.updateSettings(settings: currentSettings)
} }
@MainActor
func selectMic(_ mic: MediaDeviceId) {
self.selectedMic = mic
self.rtviClientIOS?.updateMic(micId: mic, completion: nil)
}
} }
extension CallContainerModel:RTVIClientDelegate, LLMHelperDelegate { extension CallContainerModel:RTVIClientDelegate, LLMHelperDelegate {
@@ -196,4 +215,15 @@ extension CallContainerModel:RTVIClientDelegate, LLMHelperDelegate {
} }
} }
func onAvailableMicsUpdated(mics: [MediaDeviceInfo]) {
Task { @MainActor in
self.availableMics = mics
}
}
func onMicUpdated(mic: MediaDeviceInfo?) {
Task { @MainActor in
self.selectedMic = mic?.id
}
}
} }

View File

@@ -7,23 +7,21 @@ struct SettingsView: View {
@Binding var showingSettings: Bool @Binding var showingSettings: Bool
@State private var selectedMic: MediaDeviceId? = nil
@State private var isMicEnabled: Bool = true @State private var isMicEnabled: Bool = true
@State private var backendURL: String = "" @State private var backendURL: String = ""
var body: some View { var body: some View {
let microphones = self.model.rtviClientIOS?.getAllMics() ?? []
NavigationView { NavigationView {
Form { Form {
Section(header: Text("Audio Settings")) { Section(header: Text("Audio Settings")) {
List(microphones, id: \.self.id.id) { mic in List(model.availableMics, id: \.self.id.id) { mic in
Button(action: { Button(action: {
self.selectMic(mic.id) model.selectMic(mic.id)
}) { }) {
HStack { HStack {
Text(mic.name) Text(mic.name)
Spacer() Spacer()
if mic.id == self.selectedMic { if mic.id == model.selectedMic {
Image(systemName: "checkmark") Image(systemName: "checkmark")
} }
} }
@@ -53,14 +51,9 @@ struct SettingsView: View {
} }
} }
private func selectMic(_ mic: MediaDeviceId) {
self.selectedMic = mic
self.model.rtviClientIOS?.updateMic(micId: mic, completion: nil)
}
private func saveSettings() { private func saveSettings() {
let newSettings = SettingsPreference( let newSettings = SettingsPreference(
selectedMic: selectedMic?.id, selectedMic: model.selectedMic?.id,
enableMic: isMicEnabled, enableMic: isMicEnabled,
backendURL: backendURL backendURL: backendURL
) )
@@ -69,11 +62,6 @@ struct SettingsView: View {
private func loadSettings() { private func loadSettings() {
let savedSettings = SettingsManager.getSettings() let savedSettings = SettingsManager.getSettings()
if let selectedMic = savedSettings.selectedMic {
self.selectedMic = MediaDeviceId(id: selectedMic)
} else {
self.selectedMic = nil
}
self.isMicEnabled = savedSettings.enableMic self.isMicEnabled = savedSettings.enableMic
self.backendURL = savedSettings.backendURL self.backendURL = savedSettings.backendURL
} }