audio_input.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_hbb/common.dart';
  3. import 'package:flutter_hbb/models/platform_model.dart';
  4. const _kWindowsSystemSound = 'System Sound';
  5. typedef AudioINputSetDevice = void Function(String device);
  6. typedef AudioInputBuilder = Widget Function(
  7. List<String> devices, String currentDevice, AudioINputSetDevice setDevice);
  8. class AudioInput extends StatelessWidget {
  9. final AudioInputBuilder builder;
  10. final bool isCm;
  11. final bool isVoiceCall;
  12. const AudioInput(
  13. {Key? key,
  14. required this.builder,
  15. required this.isCm,
  16. required this.isVoiceCall})
  17. : super(key: key);
  18. static String getDefault() {
  19. if (isWindows) return translate('System Sound');
  20. return '';
  21. }
  22. static Future<String> getAudioInput(bool isCm, bool isVoiceCall) {
  23. if (isVoiceCall) {
  24. return bind.getVoiceCallInputDevice(isCm: isCm);
  25. } else {
  26. return bind.mainGetOption(key: 'audio-input');
  27. }
  28. }
  29. static Future<String> getValue(bool isCm, bool isVoiceCall) async {
  30. String device = await getAudioInput(isCm, isVoiceCall);
  31. if (device.isNotEmpty) {
  32. return device;
  33. } else {
  34. return getDefault();
  35. }
  36. }
  37. static Future<void> setDevice(
  38. String device, bool isCm, bool isVoiceCall) async {
  39. if (device == getDefault()) device = '';
  40. if (isVoiceCall) {
  41. await bind.setVoiceCallInputDevice(isCm: isCm, device: device);
  42. } else {
  43. await bind.mainSetOption(key: 'audio-input', value: device);
  44. }
  45. }
  46. static Future<Map<String, Object>> getDevicesInfo(
  47. bool isCm, bool isVoiceCall) async {
  48. List<String> devices = (await bind.mainGetSoundInputs()).toList();
  49. if (isWindows) {
  50. devices.insert(0, translate(_kWindowsSystemSound));
  51. }
  52. String current = await getValue(isCm, isVoiceCall);
  53. return {'devices': devices, 'current': current};
  54. }
  55. @override
  56. Widget build(BuildContext context) {
  57. return futureBuilder(
  58. future: getDevicesInfo(isCm, isVoiceCall),
  59. hasData: (data) {
  60. String currentDevice = data['current'];
  61. List<String> devices = data['devices'] as List<String>;
  62. if (devices.isEmpty) {
  63. return const Offstage();
  64. }
  65. return builder(devices, currentDevice, (devices) {
  66. setDevice(devices, isCm, isVoiceCall);
  67. });
  68. },
  69. );
  70. }
  71. }