-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.dart
308 lines (293 loc) · 8.76 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_volume_controller/flutter_volume_controller.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _HomeState();
}
class _HomeState extends State<Home> {
AudioStream _audioStream = AudioStream.music;
AudioSessionCategory _audioSessionCategory = AudioSessionCategory.ambient;
double _currentVolume = 0.0;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (Platform.isIOS) {
await _loadIOSAudioSessionCategory();
}
if (Platform.isAndroid) {
await _loadAndroidAudioStream();
}
});
FlutterVolumeController.addListener((volume) {
setState(() {
_currentVolume = volume;
});
});
}
@override
void dispose() {
FlutterVolumeController.removeListener();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Volume Controller Example'),
),
body: ListView(
padding: const EdgeInsets.all(20),
children: [
if (Platform.isAndroid || Platform.isIOS)
Center(
child: ElevatedButton(
child: const Text('Show or hide system ui'),
onPressed: () {
FlutterVolumeController.updateShowSystemUI(
!FlutterVolumeController.showSystemUI);
_showSnackBar(
'Show system ui: ${FlutterVolumeController.showSystemUI}',
);
},
),
),
if (Platform.isAndroid) ...[
Center(
child: ElevatedButton(
child: const Text('Switch audio stream'),
onPressed: () async {
final stream = await _pickAndroidAudioStream(context);
if (stream != null) {
setState(() {
_audioStream = stream;
});
await FlutterVolumeController.setAndroidAudioStream(
stream: stream,
);
}
},
),
),
Center(
child: ElevatedButton(
child: const Text('Get audio stream'),
onPressed: () async {
final stream =
await FlutterVolumeController.getAndroidAudioStream();
_showSnackBar('Audio stream: $stream');
},
),
),
],
if (Platform.isIOS) ...[
Center(
child: ElevatedButton(
child: const Text('Switch audio session category'),
onPressed: () async {
final category = await _pickIOSAudioSessionCategory(context);
if (category != null) {
setState(() {
_audioSessionCategory = category;
});
await FlutterVolumeController.setIOSAudioSessionCategory(
category: category,
);
}
},
),
),
Center(
child: ElevatedButton(
child: const Text('Get audio session category'),
onPressed: () async {
final category = await FlutterVolumeController
.getIOSAudioSessionCategory();
_showSnackBar('Audio session category: $category');
},
),
),
],
Center(
child: ElevatedButton(
child: const Text('Get Volume'),
onPressed: () async {
final volume = await FlutterVolumeController.getVolume(
stream: _audioStream,
);
_showSnackBar('Current Volume: $volume');
},
),
),
Center(
child: ElevatedButton(
child: const Text('Set Volume to 50%'),
onPressed: () {
FlutterVolumeController.setVolume(
0.5,
stream: _audioStream,
);
},
),
),
Center(
child: ElevatedButton(
child: const Text('Raise Volume'),
onPressed: () {
FlutterVolumeController.raiseVolume(
0.2,
stream: _audioStream,
);
},
),
),
Center(
child: ElevatedButton(
child: const Text('Lower Volume'),
onPressed: () {
FlutterVolumeController.lowerVolume(
0.2,
stream: _audioStream,
);
},
),
),
Center(
child: ElevatedButton(
child: const Text('Get mute'),
onPressed: () async {
final isMuted = await FlutterVolumeController.getMute();
_showSnackBar('Muted: $isMuted');
},
),
),
Center(
child: ElevatedButton(
child: const Text('Set mute'),
onPressed: () {
FlutterVolumeController.setMute(
true,
stream: _audioStream,
);
},
),
),
Center(
child: ElevatedButton(
child: const Text('Set unmute'),
onPressed: () {
FlutterVolumeController.setMute(
false,
stream: _audioStream,
);
},
),
),
Center(
child: ElevatedButton(
child: const Text('Toggle mute'),
onPressed: () {
FlutterVolumeController.toggleMute(stream: _audioStream);
},
),
),
const SizedBox(height: 20),
Text(
'Current Volume: $_currentVolume',
textAlign: TextAlign.center,
),
if (Platform.isAndroid)
Text(
'Audio Stream: $_audioStream',
textAlign: TextAlign.center,
),
if (Platform.isIOS)
Text(
'Audio Session Category: $_audioSessionCategory',
textAlign: TextAlign.center,
),
],
),
);
}
Future<AudioStream?> _pickAndroidAudioStream(BuildContext context) async {
return await showModalBottomSheet(
context: context,
builder: (context) {
return ListView.builder(
shrinkWrap: true,
itemCount: AudioStream.values.length,
itemBuilder: (_, index) {
return ListTile(
title: Text(AudioStream.values[index].name),
onTap: () {
Navigator.of(context).maybePop(AudioStream.values[index]);
},
);
},
);
},
);
}
Future<AudioSessionCategory?> _pickIOSAudioSessionCategory(
BuildContext context,
) async {
return await showModalBottomSheet(
context: context,
builder: (context) {
return ListView.builder(
shrinkWrap: true,
itemCount: AudioSessionCategory.values.length,
itemBuilder: (_, index) {
return ListTile(
title: Text(AudioSessionCategory.values[index].name),
onTap: () {
Navigator.of(context).maybePop(
AudioSessionCategory.values[index],
);
},
);
},
);
},
);
}
void _showSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
),
);
}
Future<void> _loadIOSAudioSessionCategory() async {
final category = await FlutterVolumeController.getIOSAudioSessionCategory();
if (category != null) {
setState(() {
_audioSessionCategory = category;
});
}
}
Future<void> _loadAndroidAudioStream() async {
final audioStream = await FlutterVolumeController.getAndroidAudioStream();
if (audioStream != null) {
setState(() {
_audioStream = _audioStream;
});
}
}
}