Skip to content

Commit 0d933c3

Browse files
committed
update the initialize logic for live api demo
1 parent 91272eb commit 0d933c3

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

firebase_ai_logic_showcase/lib/demos/live_api/live_api_demo.dart

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import 'utilities/utilities.dart';
2323
import 'firebaseai_live_api_service.dart';
2424

2525
class LiveAPIDemo extends ConsumerStatefulWidget {
26-
const LiveAPIDemo({super.key});
26+
const LiveAPIDemo({super.key, this.isSelected = false});
27+
final bool isSelected;
2728

2829
@override
2930
ConsumerState<LiveAPIDemo> createState() => _LiveAPIDemoState();
@@ -36,7 +37,7 @@ class LiveAPIDemo extends ConsumerStatefulWidget {
3637
/// with the [LiveApiService] and I/O utilities.
3738
class _LiveAPIDemoState extends ConsumerState<LiveAPIDemo> {
3839
// Service for interacting with the Gemini API via Firebase AI.
39-
late final LiveApiService _liveApiService;
40+
late LiveApiService _liveApiService;
4041

4142
// Utilities for handling device I/O.
4243
late final AudioInput _audioInput = AudioInput();
@@ -46,6 +47,7 @@ class _LiveAPIDemoState extends ConsumerState<LiveAPIDemo> {
4647
// Initialization flags.
4748
bool _audioIsInitialized = false;
4849
bool _videoIsInitialized = false;
50+
static bool _hasBeenSelected = false;
4951

5052
// UI State flags.
5153
bool _isConnecting = false; // True when setting up the Gemini session.
@@ -56,13 +58,32 @@ class _LiveAPIDemoState extends ConsumerState<LiveAPIDemo> {
5658
@override
5759
void initState() {
5860
super.initState();
59-
_liveApiService = LiveApiService(
60-
audioOutput: _audioOutput,
61-
ref: ref, // Pass the ref to the service
62-
onImageLoadingChange: _onImageLoadingChange,
63-
onImageGenerated: _onImageGenerated,
64-
onError: _showErrorSnackBar,
65-
);
61+
WidgetsBinding.instance.addPostFrameCallback((_) {
62+
_checkAndInitializeIO();
63+
});
64+
}
65+
66+
@override
67+
void didUpdateWidget(LiveAPIDemo oldWidget) {
68+
super.didUpdateWidget(oldWidget);
69+
if (widget.isSelected != oldWidget.isSelected) {
70+
_checkAndInitializeIO();
71+
}
72+
}
73+
74+
Future<void> _checkAndInitializeIO() async {
75+
if (widget.isSelected && !_hasBeenSelected) {
76+
_hasBeenSelected = true;
77+
await _initializeAudio();
78+
await _initializeVideo();
79+
_liveApiService = LiveApiService(
80+
audioOutput: _audioOutput,
81+
ref: ref, // Pass the ref to the service
82+
onImageLoadingChange: _onImageLoadingChange,
83+
onImageGenerated: _onImageGenerated,
84+
onError: _showErrorSnackBar,
85+
);
86+
}
6687
}
6788

6889
@override
@@ -110,14 +131,6 @@ class _LiveAPIDemoState extends ConsumerState<LiveAPIDemo> {
110131
}
111132

112133
Future<void> startCall() async {
113-
// Initialize audio and video streams if they haven't been already.
114-
if (!_audioIsInitialized) {
115-
await _initializeAudio();
116-
}
117-
if (!_videoIsInitialized) {
118-
await _initializeVideo();
119-
}
120-
121134
// Initialize the camera controller here to ensure it's fresh for each call.
122135
// This prevents a bug where the camera preview freezes on subsequent calls.
123136
if (_videoIsInitialized) {

firebase_ai_logic_showcase/lib/flutter_firebase_ai_demo.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class _DemoHomeScreenState extends State<DemoHomeScreen> {
3838
Widget build(BuildContext context) {
3939
final List<Widget> demoPages = <Widget>[
4040
const ChatDemo(),
41-
const LiveAPIDemo(),
41+
LiveAPIDemo(isSelected: _selectedIndex == 1),
4242
const MultimodalDemo(),
4343
ChatDemoNano(isSelected: _selectedIndex == 3),
4444
];

0 commit comments

Comments
 (0)