Procházet zdrojové kódy

新增一键检测所有设备&&麦克风自动完成检测

gavin.chen před 2 roky
rodič
revize
70bfc635a4

+ 21 - 9
lib/hardware_detection/controller.dart

@@ -78,8 +78,10 @@ class HardwareDetectionController extends GetxController {
   }
 
   // 刷新设备列表
-  void refreshDevices() {
-    ///TODO 刷新设备列表
+  Future<void> refreshAllDevices() async {
+    await refreshCameraList();
+    await refreshMicrophoneList();
+    await refreshSpeakerList();
   }
 
   /// 设置当前选中的摄像头可用性
@@ -139,6 +141,14 @@ class HardwareDetectionController extends GetxController {
     _stopPlaySound();
   }
 
+  /// 开始检测所有设备
+  Future<void> startDetectingAll() async {
+    await refreshAllDevices();
+    if (state.cameraList.isNotEmpty) startDetectingCamera();
+    if (state.microphoneList.isNotEmpty) startDetectingMicrophone();
+    if (state.speakerList.isNotEmpty) startDetectingSpeaker();
+  }
+
   /// 选择摄像头
   void selectCameraById(String deviceId) {
     if (state.currentCamera != null && deviceId == state.currentCamera!.id)
@@ -151,16 +161,14 @@ class HardwareDetectionController extends GetxController {
       }
     });
     if (state.currentCamera == null) return;
-
-    /// 如果当前正在检测摄像头,则刷新
     if (state.detectingCamera) {
       _stopCamera();
       _openSelectedCamera();
     }
   }
 
-  /// 选择麦克风 [web 无法修改麦克风]
-  void selectMicrophoneById(String deviceId) {
+  /// 选择麦克风
+  Future<void> selectMicrophoneById(String deviceId) async {
     state.microphoneList.forEach((element) {
       if (element.id == deviceId) {
         state.currentMicrophone = element;
@@ -169,6 +177,13 @@ class HardwareDetectionController extends GetxController {
       }
     });
     if (state.currentMicrophone == null) return;
+    // TRTCCloud trtcCloud = (await TRTCCloud.sharedInstance())!;
+    // TXDeviceManager deviceManager = trtcCloud.getDeviceManager();
+    // deviceManager.setCurrentDevice(0, state.currentMicrophone!.id);
+    // if (state.detectingMicrophone) {
+    //   _stopRecordMicrophone();
+    //   _startRecordMicrophone();
+    // }
   }
 
   /// 选择扬声器
@@ -197,7 +212,6 @@ class HardwareDetectionController extends GetxController {
       }
     }
     state.deviceList.addAll(state.speakerList);
-    print("当前总设备数量: ${state.deviceList.length}");
   }
 
   /// 刷新摄像头列表
@@ -214,7 +228,6 @@ class HardwareDetectionController extends GetxController {
       }
     }
     state.deviceList.addAll(state.cameraList);
-    print("当前总设备数量: ${state.deviceList.length}");
   }
 
   /// 刷新麦克风列表
@@ -231,7 +244,6 @@ class HardwareDetectionController extends GetxController {
       }
     }
     state.deviceList.addAll(state.microphoneList);
-    print("当前总设备数量: ${state.deviceList.length}");
   }
 
   /// 调用 TRTC 获取摄像头列表

+ 4 - 0
lib/hardware_detection/view.dart

@@ -35,6 +35,10 @@ class HardwareDetectionPage extends GetView<HardwareDetectionController> {
             SizedBox(height: 20),
             MicrophoneUI(),
             SizedBox(height: 20),
+            ElevatedButton(
+              onPressed: controller.startDetectingAll,
+              child: Text("检测所有设备"),
+            )
           ],
         ),
       ),

+ 21 - 16
lib/hardware_detection/widgets/camera_ui.dart

@@ -9,24 +9,29 @@ class CameraUI extends GetView<HardwareDetectionController> {
   @override
   Widget build(BuildContext context) {
     return Obx(
-      () => ExpandableContainer(
-        expanded: controller.state.isDisplayCameraWindow,
-        child: controller.state.isDisplayCameraWindow
-            ? CameraPreview(controller.cameraController)
-            : Container(
-                decoration: BoxDecoration(
-                  border: Border.all(color: Colors.grey[300]!),
-                  borderRadius: BorderRadius.circular(5),
-                  color: Colors.grey[200],
-                ),
-                child: Center(
-                  child: Icon(
-                    Icons.photo_camera_outlined,
-                    color: Colors.black54,
+      () {
+        if (controller.state.cameraList.isEmpty) {
+          return Container();
+        }
+        return ExpandableContainer(
+          expanded: controller.state.isDisplayCameraWindow,
+          child: controller.state.isDisplayCameraWindow
+              ? CameraPreview(controller.cameraController)
+              : Container(
+                  decoration: BoxDecoration(
+                    border: Border.all(color: Colors.grey[300]!),
+                    borderRadius: BorderRadius.circular(5),
+                    color: Colors.grey[200],
+                  ),
+                  child: Center(
+                    child: Icon(
+                      Icons.photo_camera_outlined,
+                      color: Colors.black54,
+                    ),
                   ),
                 ),
-              ),
-      ),
+        );
+      },
     );
   }
 }

+ 8 - 4
lib/hardware_detection/widgets/microphone_ui.dart

@@ -6,11 +6,15 @@ import 'widgets.dart';
 class MicrophoneUI extends GetView<HardwareDetectionController> {
   @override
   Widget build(BuildContext context) {
-    return Obx(
-      () => controller.state.isDisplayMicrophoneWave
+    return Obx(() {
+      if (controller.state.microphoneList.isEmpty) {
+        return Container();
+      }
+      return controller.state.isDisplayMicrophoneWave
           ? MicrophoneWave(
               isRecording: controller.state.detectingMicrophone,
               recorder: controller.flutterRecorder,
+              onAutoDetectPass: () => controller.setMicrophoneAvailable(true),
               width: 250.0,
               height: 60.0,
             )
@@ -27,7 +31,7 @@ class MicrophoneUI extends GetView<HardwareDetectionController> {
                   ),
                 ),
               ),
-            ),
-    );
+            );
+    });
   }
 }

+ 12 - 2
lib/hardware_detection/widgets/microphone_wave.dart

@@ -5,6 +5,7 @@ import 'package:flutter_sound/flutter_sound.dart';
 /// 麦克风输入可视化
 class MicrophoneWave extends StatefulWidget {
   final FlutterSoundRecorder recorder;
+  final VoidCallback onAutoDetectPass;
   final double width;
   final double height;
   final bool isRecording;
@@ -12,6 +13,7 @@ class MicrophoneWave extends StatefulWidget {
   MicrophoneWave({
     required this.recorder,
     required this.isRecording,
+    required this.onAutoDetectPass,
     this.width = 250.0,
     this.height = 40.0,
   });
@@ -22,10 +24,12 @@ class MicrophoneWave extends StatefulWidget {
 
 class _MicrophoneWaveState extends State<MicrophoneWave>
     with SingleTickerProviderStateMixin {
-  StreamSubscription? _recorderSubscription;
   late AnimationController _controller;
   late Animation<double> _animation;
+  StreamSubscription? _recorderSubscription;
   List<double> _loudnessValues = [];
+  int autoDetectCount = 0;
+  static const int autoDetectThreshold = 10;
 
   @override
   void initState() {
@@ -41,12 +45,18 @@ class _MicrophoneWaveState extends State<MicrophoneWave>
 
     /// 如果没有分贝数据,请参考 https://github.com/Canardoux/flutter_sound/issues/838
     /// 将  flutter_sound_web 的 flutter_sound_recorder.js 从 flutter_sound_web/js/flutter_sound 复制到 flutter_sound_web/src
-    /// 监听 onProgress 回调
     _recorderSubscription =
         widget.recorder.onProgress!.listen((RecordingDisposition e) {
       /// 输出响度,响度范围是0-120
       /// 0表示没有声音,120表示最大声音
       final loudness = ((e.decibels ?? 0) + 5) / 120;
+      if (loudness > 0.2) {
+        autoDetectCount++;
+      }
+      if (autoDetectCount > autoDetectThreshold) {
+        autoDetectCount = 0;
+        widget.onAutoDetectPass.call();
+      }
       setState(() {
         _loudnessValues.add(loudness);
       });

+ 13 - 8
lib/hardware_detection/widgets/speaker_ui.dart

@@ -6,13 +6,18 @@ import 'speaker_wave.dart';
 class SpeakerUI extends GetView<HardwareDetectionController> {
   @override
   Widget build(BuildContext context) {
-    return Obx(() => controller.state.isDisplaySpeakerWave
-        ? SpeakerWave(
-            audioPlayer: controller.flutterSound,
-            isPlaying: controller.state.detectingSpeaker,
-            width: 250.0,
-            height: 40.0,
-          )
-        : Container());
+    return Obx(() {
+      if (controller.state.speakerList.isEmpty) {
+        return Container();
+      }
+      return controller.state.isDisplaySpeakerWave
+          ? SpeakerWave(
+              audioPlayer: controller.flutterSound,
+              isPlaying: controller.state.detectingSpeaker,
+              width: 250.0,
+              height: 40.0,
+            )
+          : Container();
+    });
   }
 }