|
1 | 1 |
|
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:sound_volume_view/sound_volume_view.dart'; |
| 4 | + |
| 5 | +/// This file is a part of SoundVolumeView (https://github.com/DomingoMG/sound_volume_view.dart). |
| 6 | +/// |
| 7 | +/// Copyright (c) 2022, Domingo Montesdeoca González <DomingoMG97@gmail.com>. |
| 8 | +/// All rights reserved. |
| 9 | +/// Use of this source code is governed by MIT license that can be found in the LICENSE file. |
| 10 | +/// |
| 11 | +void main() => runApp(const SoundVolumeViewApp()); |
| 12 | + |
| 13 | +class SoundVolumeViewApp extends StatelessWidget { |
| 14 | + const SoundVolumeViewApp({Key? key}) : super(key: key); |
| 15 | + |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return const MaterialApp(title: 'Sound Volume View', home: HomePage()); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +class HomePage extends StatefulWidget { |
| 23 | + const HomePage({Key? key}) : super(key: key); |
| 24 | + |
| 25 | + @override |
| 26 | + _HomePageState createState() => _HomePageState(); |
| 27 | +} |
| 28 | + |
| 29 | +class _HomePageState extends State<HomePage> { |
| 30 | + SoundVolumeView soundVolumeView = |
| 31 | + SoundVolumeView(softwarePath: 'D:\\Trabajos\\sound_volume_view\\'); |
| 32 | + |
| 33 | + @override |
| 34 | + void initState() { |
| 35 | + _initialSoftware(); |
| 36 | + super.initState(); |
| 37 | + } |
| 38 | + |
| 39 | + Future<void> _initialSoftware() async { |
| 40 | + await soundVolumeView.getDevices; |
| 41 | + if (mounted) { |
| 42 | + setState(() {}); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + Widget build(BuildContext context) { |
| 48 | + final ScrollController _scrollController1 = ScrollController(); |
| 49 | + final ScrollController _scrollController2 = ScrollController(); |
| 50 | + final ScrollController _scrollController3 = ScrollController(); |
| 51 | + return Scaffold( |
| 52 | + appBar: AppBar( |
| 53 | + title: const Text('Sound Volume View'), |
| 54 | + ), |
| 55 | + body: Column( |
| 56 | + children: [ |
| 57 | + ListTile( |
| 58 | + title: const Text( |
| 59 | + 'Example of sound volume view with basic functionalities', |
| 60 | + style: TextStyle(fontSize: 25)), |
| 61 | + subtitle: const Text('DomingoMG'), |
| 62 | + trailing: IconButton( |
| 63 | + icon: const Icon(Icons.sync), |
| 64 | + tooltip: 'Refresh', |
| 65 | + onPressed: () async { |
| 66 | + await soundVolumeView.getDevices; |
| 67 | + setState(() {}); |
| 68 | + }, |
| 69 | + )), |
| 70 | + Expanded( |
| 71 | + child: Row( |
| 72 | + children: [ |
| 73 | + Expanded( |
| 74 | + child: Card( |
| 75 | + color: Colors.red.shade100, |
| 76 | + child: Column( |
| 77 | + children: [ |
| 78 | + const ListTile( |
| 79 | + title: Text('Capture Devices'), |
| 80 | + subtitle: |
| 81 | + Text('Input devices connected to your system'), |
| 82 | + ), |
| 83 | + Expanded( |
| 84 | + child: ListView.builder( |
| 85 | + controller: _scrollController1, |
| 86 | + itemCount: soundVolumeView.captureDevices.length, |
| 87 | + scrollDirection: Axis.vertical, |
| 88 | + itemBuilder: (BuildContext context, int index) => |
| 89 | + Card( |
| 90 | + child: ListTile( |
| 91 | + title: Text(soundVolumeView |
| 92 | + .captureDevices[index].name!), |
| 93 | + subtitle: Text(soundVolumeView |
| 94 | + .captureDevices[index].deviceName!), |
| 95 | + trailing: Wrap( |
| 96 | + children: [ |
| 97 | + IconButton( |
| 98 | + tooltip: soundVolumeView |
| 99 | + .captureDevices[index] |
| 100 | + .muted == |
| 101 | + 'No' |
| 102 | + ? 'Muted' |
| 103 | + : 'unMute', |
| 104 | + icon: soundVolumeView |
| 105 | + .captureDevices[index] |
| 106 | + .muted == |
| 107 | + 'Yes' |
| 108 | + ? const Icon(Icons.volume_off, |
| 109 | + color: Colors.red) |
| 110 | + : const Icon(Icons.volume_up, |
| 111 | + color: Colors.green), |
| 112 | + onPressed: () async { |
| 113 | + soundVolumeView.setVolume( |
| 114 | + soundVolumeView |
| 115 | + .captureDevices[index], |
| 116 | + 100); |
| 117 | + if (soundVolumeView |
| 118 | + .captureDevices[index].muted == |
| 119 | + 'Yes') { |
| 120 | + await soundVolumeView.unMute( |
| 121 | + soundVolumeView |
| 122 | + .captureDevices[index]); |
| 123 | + } else { |
| 124 | + await soundVolumeView.mute( |
| 125 | + soundVolumeView |
| 126 | + .captureDevices[index]); |
| 127 | + } |
| 128 | + setState(() {}); |
| 129 | + }, |
| 130 | + ), |
| 131 | + IconButton( |
| 132 | + icon: const Icon(Icons.record_voice_over, |
| 133 | + color: Colors.black), |
| 134 | + tooltip: 'Listen', |
| 135 | + onPressed: () async { |
| 136 | + if (soundVolumeView |
| 137 | + .captureDevices[index].listen) { |
| 138 | + soundVolumeView.captureDevices[index] |
| 139 | + .listen = false; |
| 140 | + } else { |
| 141 | + soundVolumeView.captureDevices[index] |
| 142 | + .listen = true; |
| 143 | + } |
| 144 | + await soundVolumeView |
| 145 | + .setListenToThisDevice( |
| 146 | + soundVolumeView |
| 147 | + .captureDevices[index], |
| 148 | + listen: soundVolumeView |
| 149 | + .captureDevices[index] |
| 150 | + .listen); |
| 151 | + }, |
| 152 | + ), |
| 153 | + SizedBox( |
| 154 | + width: MediaQuery.of(context).size.width * |
| 155 | + 0.07, |
| 156 | + height: 90, |
| 157 | + child: DropdownButtonFormField( |
| 158 | + isExpanded: true, |
| 159 | + items: soundVolumeView.outputDevices |
| 160 | + .map((output) => DropdownMenuItem( |
| 161 | + child: Text(output.name!), |
| 162 | + value: output.itemID, |
| 163 | + )) |
| 164 | + .toList(), |
| 165 | + onChanged: (String? value) async { |
| 166 | + Device device = soundVolumeView |
| 167 | + .outputDevices |
| 168 | + .firstWhere((element) => |
| 169 | + element.itemID == value); |
| 170 | + await soundVolumeView |
| 171 | + .setPlaybackThroughDevice( |
| 172 | + soundVolumeView |
| 173 | + .captureDevices[index], |
| 174 | + device); |
| 175 | + }, |
| 176 | + ), |
| 177 | + ) |
| 178 | + ], |
| 179 | + ), |
| 180 | + )), |
| 181 | + ), |
| 182 | + ), |
| 183 | + ], |
| 184 | + ), |
| 185 | + ), |
| 186 | + ), |
| 187 | + Expanded( |
| 188 | + child: Card( |
| 189 | + color: Colors.blue.shade100, |
| 190 | + child: Column( |
| 191 | + children: [ |
| 192 | + const ListTile( |
| 193 | + title: Text('Application Devices'), |
| 194 | + subtitle: Text('Output devices via app'), |
| 195 | + ), |
| 196 | + Expanded( |
| 197 | + child: ListView.builder( |
| 198 | + controller: _scrollController2, |
| 199 | + itemCount: |
| 200 | + soundVolumeView.applicationDevices.length, |
| 201 | + scrollDirection: Axis.vertical, |
| 202 | + itemBuilder: (BuildContext context, int index) => |
| 203 | + Card( |
| 204 | + child: ListTile( |
| 205 | + title: Text(soundVolumeView |
| 206 | + .applicationDevices[index].name!), |
| 207 | + subtitle: Text(soundVolumeView |
| 208 | + .applicationDevices[index].deviceName!), |
| 209 | + trailing: Wrap( |
| 210 | + children: [ |
| 211 | + IconButton( |
| 212 | + icon: soundVolumeView |
| 213 | + .applicationDevices[index] |
| 214 | + .muted == |
| 215 | + 'Yes' |
| 216 | + ? const Icon(Icons.volume_off, |
| 217 | + color: Colors.red) |
| 218 | + : const Icon(Icons.volume_up, |
| 219 | + color: Colors.green), |
| 220 | + onPressed: () async { |
| 221 | + if (soundVolumeView |
| 222 | + .applicationDevices[index] |
| 223 | + .muted == |
| 224 | + 'Yes') { |
| 225 | + await soundVolumeView.unMute( |
| 226 | + soundVolumeView |
| 227 | + .applicationDevices[index]); |
| 228 | + } else { |
| 229 | + await soundVolumeView.mute( |
| 230 | + soundVolumeView |
| 231 | + .applicationDevices[index]); |
| 232 | + } |
| 233 | + setState(() {}); |
| 234 | + }, |
| 235 | + ), |
| 236 | + SizedBox( |
| 237 | + width: MediaQuery.of(context).size.width * |
| 238 | + 0.07, |
| 239 | + height: 90, |
| 240 | + child: DropdownButtonFormField( |
| 241 | + isExpanded: true, |
| 242 | + items: soundVolumeView.outputDevices |
| 243 | + .map((output) => DropdownMenuItem( |
| 244 | + child: Text(output.name!), |
| 245 | + value: output.itemID, |
| 246 | + )) |
| 247 | + .toList(), |
| 248 | + onChanged: (String? value) async { |
| 249 | + Device device = soundVolumeView |
| 250 | + .outputDevices |
| 251 | + .firstWhere((element) => |
| 252 | + element.itemID == value); |
| 253 | + await soundVolumeView.setAppDefault( |
| 254 | + soundVolumeView |
| 255 | + .applicationDevices[index], |
| 256 | + device, |
| 257 | + defaultType: DefaultType.all); |
| 258 | + }, |
| 259 | + ), |
| 260 | + ) |
| 261 | + ], |
| 262 | + ), |
| 263 | + )), |
| 264 | + ), |
| 265 | + ), |
| 266 | + ], |
| 267 | + ), |
| 268 | + ), |
| 269 | + ), |
| 270 | + Expanded( |
| 271 | + child: Card( |
| 272 | + color: Colors.green.shade100, |
| 273 | + child: Column( |
| 274 | + children: [ |
| 275 | + const ListTile( |
| 276 | + title: Text('Output devices'), |
| 277 | + subtitle: |
| 278 | + Text('Output devices found on your system'), |
| 279 | + ), |
| 280 | + Expanded( |
| 281 | + child: ListView.builder( |
| 282 | + controller: _scrollController3, |
| 283 | + itemCount: soundVolumeView.outputDevices.length, |
| 284 | + scrollDirection: Axis.vertical, |
| 285 | + itemBuilder: (BuildContext context, int index) => |
| 286 | + Card( |
| 287 | + child: ListTile( |
| 288 | + title: Text( |
| 289 | + soundVolumeView.outputDevices[index].name!), |
| 290 | + subtitle: Text(soundVolumeView |
| 291 | + .outputDevices[index].deviceName!), |
| 292 | + trailing: Wrap( |
| 293 | + children: [ |
| 294 | + IconButton( |
| 295 | + icon: soundVolumeView |
| 296 | + .outputDevices[index].muted == |
| 297 | + 'Yes' |
| 298 | + ? const Icon(Icons.volume_off, |
| 299 | + color: Colors.red) |
| 300 | + : const Icon(Icons.volume_up, |
| 301 | + color: Colors.green), |
| 302 | + onPressed: () async { |
| 303 | + if (soundVolumeView |
| 304 | + .outputDevices[index].muted == |
| 305 | + 'Yes') { |
| 306 | + await soundVolumeView.unMute( |
| 307 | + soundVolumeView |
| 308 | + .outputDevices[index]); |
| 309 | + } else { |
| 310 | + await soundVolumeView.mute( |
| 311 | + soundVolumeView |
| 312 | + .outputDevices[index]); |
| 313 | + } |
| 314 | + setState(() {}); |
| 315 | + }, |
| 316 | + ), |
| 317 | + SizedBox( |
| 318 | + width: MediaQuery.of(context).size.width * |
| 319 | + 0.08, |
| 320 | + height: 90, |
| 321 | + child: |
| 322 | + DropdownButtonFormField<DefaultType>( |
| 323 | + isExpanded: true, |
| 324 | + items: const [ |
| 325 | + DropdownMenuItem<DefaultType>( |
| 326 | + child: Text(''), |
| 327 | + value: null, |
| 328 | + ), |
| 329 | + DropdownMenuItem<DefaultType>( |
| 330 | + child: Text('All'), |
| 331 | + value: DefaultType.all, |
| 332 | + ), |
| 333 | + DropdownMenuItem<DefaultType>( |
| 334 | + child: Text('Communications'), |
| 335 | + value: DefaultType.communications, |
| 336 | + ), |
| 337 | + DropdownMenuItem<DefaultType>( |
| 338 | + child: Text('Console'), |
| 339 | + value: DefaultType.console, |
| 340 | + ), |
| 341 | + DropdownMenuItem<DefaultType>( |
| 342 | + child: Text('Multimedia'), |
| 343 | + value: DefaultType.multimedia, |
| 344 | + ), |
| 345 | + ], |
| 346 | + onChanged: |
| 347 | + (DefaultType? defaultType) async { |
| 348 | + if (defaultType is DefaultType) { |
| 349 | + await soundVolumeView.setDefault( |
| 350 | + soundVolumeView |
| 351 | + .outputDevices[index], |
| 352 | + defaultType: defaultType); |
| 353 | + } |
| 354 | + }, |
| 355 | + ), |
| 356 | + ) |
| 357 | + ], |
| 358 | + ), |
| 359 | + )), |
| 360 | + ), |
| 361 | + ), |
| 362 | + ], |
| 363 | + ), |
| 364 | + ), |
| 365 | + ), |
| 366 | + ], |
| 367 | + ), |
| 368 | + ) |
| 369 | + ], |
| 370 | + )); |
| 371 | + } |
| 372 | +} |
0 commit comments