Skip to content

Commit 985c56d

Browse files
authored
example app: remember settings (#934)
Remember: * Codec settings * Publish cam / mic
1 parent 65d48b5 commit 985c56d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

example/lib/pages/connect.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class _ConnectPageState extends State<ConnectPage> {
2929
static const _storeKeyE2EE = 'e2ee';
3030
static const _storeKeySharedKey = 'shared-key';
3131
static const _storeKeyMultiCodec = 'multi-codec';
32+
static const _storeKeyPreferredCodec = 'preferred-codec';
3233

3334
final _uriCtrl = TextEditingController();
3435
final _tokenCtrl = TextEditingController();
@@ -96,6 +97,7 @@ class _ConnectPageState extends State<ConnectPage> {
9697
_dynacast = prefs.getBool(_storeKeyDynacast) ?? true;
9798
_e2ee = prefs.getBool(_storeKeyE2EE) ?? false;
9899
_multiCodec = prefs.getBool(_storeKeyMultiCodec) ?? false;
100+
_preferredCodec = prefs.getString(_storeKeyPreferredCodec) ?? 'VP8';
99101
});
100102
}
101103

@@ -110,6 +112,7 @@ class _ConnectPageState extends State<ConnectPage> {
110112
await prefs.setBool(_storeKeyDynacast, _dynacast);
111113
await prefs.setBool(_storeKeyE2EE, _e2ee);
112114
await prefs.setBool(_storeKeyMultiCodec, _multiCodec);
115+
await prefs.setString(_storeKeyPreferredCodec, _preferredCodec);
113116
}
114117

115118
Future<void> _connect(BuildContext ctx) async {
@@ -317,10 +320,11 @@ class _ConnectPageState extends State<ConnectPage> {
317320
color: Colors.blueAccent,
318321
),
319322
onChanged: (String? value) {
320-
// This is called when the user selects an item.
323+
if (value == null) return;
321324
setState(() {
322-
_preferredCodec = value!;
325+
_preferredCodec = value;
323326
});
327+
unawaited(_writePrefs());
324328
},
325329
items: ['Preferred Codec', 'AV1', 'VP9', 'VP8', 'H264', 'H265']
326330
.map<DropdownMenuItem<String>>((String value) {

example/lib/pages/prejoin.dart

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:dropdown_button2/dropdown_button2.dart';
55
import 'package:flutter/material.dart';
66
import 'package:livekit_client/livekit_client.dart';
77
import 'package:livekit_example/exts.dart';
8+
import 'package:shared_preferences/shared_preferences.dart';
89

910
import '../theme.dart';
1011
import 'room.dart';
@@ -43,6 +44,9 @@ class PreJoinPage extends StatefulWidget {
4344
}
4445

4546
class _PreJoinPageState extends State<PreJoinPage> {
47+
static const _prefKeyEnableVideo = 'prejoin-enable-video';
48+
static const _prefKeyEnableAudio = 'prejoin-enable-audio';
49+
4650
List<MediaDevice> _audioInputs = [];
4751
List<MediaDevice> _videoInputs = [];
4852
StreamSubscription? _subscription;
@@ -60,8 +64,14 @@ class _PreJoinPageState extends State<PreJoinPage> {
6064
@override
6165
void initState() {
6266
super.initState();
67+
unawaited(_initStateAsync());
68+
}
69+
70+
Future<void> _initStateAsync() async {
71+
await _readPrefs();
6372
_subscription = Hardware.instance.onDeviceChange.stream.listen(_loadDevices);
64-
unawaited(Hardware.instance.enumerateDevices().then(_loadDevices));
73+
final devices = await Hardware.instance.enumerateDevices();
74+
await _loadDevices(devices);
6575
}
6676

6777
@override
@@ -70,7 +80,7 @@ class _PreJoinPageState extends State<PreJoinPage> {
7080
super.deactivate();
7181
}
7282

73-
void _loadDevices(List<MediaDevice> devices) async {
83+
Future<void> _loadDevices(List<MediaDevice> devices) async {
7484
_audioInputs = devices.where((d) => d.kind == 'audioinput').toList();
7585
_videoInputs = devices.where((d) => d.kind == 'videoinput').toList();
7686

@@ -98,6 +108,7 @@ class _PreJoinPageState extends State<PreJoinPage> {
98108

99109
Future<void> _setEnableVideo(value) async {
100110
_enableVideo = value;
111+
await _writePrefs();
101112
if (!_enableVideo) {
102113
await _videoTrack?.stop();
103114
_videoTrack = null;
@@ -109,6 +120,7 @@ class _PreJoinPageState extends State<PreJoinPage> {
109120

110121
Future<void> _setEnableAudio(value) async {
111122
_enableAudio = value;
123+
await _writePrefs();
112124
if (!_enableAudio) {
113125
await _audioTrack?.stop();
114126
_audioTrack = null;
@@ -119,6 +131,7 @@ class _PreJoinPageState extends State<PreJoinPage> {
119131
}
120132

121133
Future<void> _changeLocalAudioTrack() async {
134+
if (!_enableAudio) return;
122135
if (_audioTrack != null) {
123136
await _audioTrack!.stop();
124137
_audioTrack = null;
@@ -135,6 +148,7 @@ class _PreJoinPageState extends State<PreJoinPage> {
135148
}
136149

137150
Future<void> _changeLocalVideoTrack() async {
151+
if (!_enableVideo) return;
138152
if (_videoTrack != null) {
139153
await _videoTrack!.stop();
140154
_videoTrack = null;
@@ -249,6 +263,20 @@ class _PreJoinPageState extends State<PreJoinPage> {
249263
Navigator.of(context).pop();
250264
}
251265

266+
Future<void> _readPrefs() async {
267+
final prefs = await SharedPreferences.getInstance();
268+
setState(() {
269+
_enableVideo = prefs.getBool(_prefKeyEnableVideo) ?? true;
270+
_enableAudio = prefs.getBool(_prefKeyEnableAudio) ?? true;
271+
});
272+
}
273+
274+
Future<void> _writePrefs() async {
275+
final prefs = await SharedPreferences.getInstance();
276+
await prefs.setBool(_prefKeyEnableVideo, _enableVideo);
277+
await prefs.setBool(_prefKeyEnableAudio, _enableAudio);
278+
}
279+
252280
@override
253281
Widget build(BuildContext context) {
254282
return Scaffold(

0 commit comments

Comments
 (0)